redux

React28Redux成员及其数据流

荒凉一梦 提交于 2020-01-08 01:07:00
actions actions其实是描述操作的对象,我们调用dispath时需要传入此对象 store store是整个应用的数据存储仓库,把我们全局管理的状态数据存储起来 它就是我们的后勤保证,专门管理后勤数据 reducers reducers接收actions并更新store 注意:redux是一个单独的数据流框架,跟react并没有直接的联系,我们也可以在JQ或其它复杂项目里面使用redux进行数据管理,当我们不知道是否应该用redux的时候,我们都是不需要的,因为只有我们很肯定redux能帮助我们管理好复杂项目数据流的时候它才能发挥它的威力,简单的项目我们只需要state+props+context就够了 接下来看一下Redux数据流的走向 注解: 组件内调用dispatch=>dispatch把actios传入store=>store接收旧数据和拿的action再传入Reducers=>Reducers根据action对旧数据进行操作然后返回新数据给store=>store把新数据再传回组件内 Sore好像是一个工厂,Reducers好像一个工人一样,我们把原材料委托给工厂,然后告诉工厂我们需要什么功能的产品,工厂再根据客户的需求对拿到的原材料进行加工处理,然后把做好的产品放进工厂里的仓库,之后工厂再从仓库里把做好的新产品交付客户。 来源: https://www

State is returning new state without touching the state REACT/REDUX

▼魔方 西西 提交于 2020-01-07 06:53:32
问题 Before I start posting the code I will explain what I want to happen and what is happening. I have been working with this all day and finally got something to work. The problem is that I have no clue why it works and if it's the best way to do it. What I want to happen is when a User clicks delete on a comment I want the comment to be delete from the current post on the back end and from the state on the front end. UPDATE: I did not really add enough to show what I was talking about. When a

Map a route parameter to under a Redux store's key

霸气de小男生 提交于 2020-01-07 06:27:07
问题 There is a "location" key in the state object which is used by multiple components as data source. In the URL (very similar to google maps) I have a parameter called "location" which is a coordinate. My goal is to map that value (with some modification) to the state's "location" key. How to do that? UPDATE The only way I could imagine is to create a middleware and react to route actions, extract the parameters from the URL somehow, then dispatch a new action that will be processed by a

Sum of a number for each element in an array [duplicate]

空扰寡人 提交于 2020-01-07 04:08:10
问题 This question already has answers here : Strange javascript addition problem (2 answers) Closed 2 years ago . I am working in a redux function where i want for each element in array the sum with n number This is the code let neWd = array.map(x => { if (x === 'M' || x === 'L'){ return x; }else{ return x + 5; } }).join(' ') At the moment return x + 5 is adding the 5 number to any element of the array but not the sum. How can i achieve it? 回答1: Assuming you have a string and it is splitted with

Should these be components or containers in Reactjs + Redux

╄→гoц情女王★ 提交于 2020-01-07 02:18:08
问题 I am trying to implement what I learned into a site. I am building my first part of my site the "nav bar" Along the top it has logo and 2 links(home button, logout buttom) and a side nav(MaterilizeCss sidenav) bar(turns into hamburger menu on mobile). Now the side nav has this List of all students courses(once they click on a course it will load up that course in the middle of the website container) Under the list of their courses is an "add course" button to add more courses. Now I am

为什么在Redux中我们需要中间件来实现异步流?

浪子不回头ぞ 提交于 2020-01-07 01:54:09
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 根据文档, “没有中间件,Redux存储仅支持同步数据流” 。 我不明白为什么会这样。 为什么容器组件不能调用异步API,然后 dispatch 操作? 例如,想象一个简单的UI:一个字段和一个按钮。 当用户按下按钮时,该字段将填充来自远程服务器的数据。 import * as React from 'react'; import * as Redux from 'redux'; import { Provider, connect } from 'react-redux'; const ActionTypes = { STARTED_UPDATING: 'STARTED_UPDATING', UPDATED: 'UPDATED' }; class AsyncApi { static getFieldValue() { const promise = new Promise((resolve) => { setTimeout(() => { resolve(Math.floor(Math.random() * 100)); }, 1000); }); return promise; } } class App extends React.Component { render() { return ( <div>

redux-promise和redux-thunk结合体 redux-promise-thunk (适用于redux-action)

与世无争的帅哥 提交于 2020-01-07 00:25:33
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> github地址: redux-promise-thunk 之前redux开发中用的redux-thunk第三方中间件,既能解决redux的异步问题,又能在action执行中获得dispatch和getState(),给开发带来很多方便。 后来进入别的项目,那个项目异步用的是redux-promise和redux-action结合。由于某些业务需要在一个action中调用dipatch,就想着redux-promise和redux-thunk结合下,就自己做了一个。 代码特别简单。 原来的用法:(redux-promise和redux-action) createAction('FETCH_THING', async id => { const result = await somePromise; return result.someValue; }); **新的用法:redux-promise-thunk和redux-action ** ` createAction('FETCH_THING', async id => { const result = await somePromise; return (dispatch, getState) => { dispatch(someData);

redux

我们两清 提交于 2020-01-06 20:55:43
Redux学习   一、什么是redux?       1)Redux由Dan Abramov在2015年创建的科技术语。是受2014年Facebook的Flux架构以及函数式编程语言Elm启发。很快,Redux因其简单易学体积小在短时间内成为最热门的前端架构。       2)Redux对于JavaScript应用而言是一个可预测状态的容器。换言之,它是一个应用数据流框架,而不是传统的像underscore.js或者AngularJs那样的库或者框架。         Redux最主要是用作应用状态的管理。简言之,Redux用一个单独的常量状态树(对象)保存这一整个应用的状态,这个对象不能直接被改变。当一些数据变         化了,一个新的对象就会被创建(使用actions和reducers)   二、用redux的好处       在应用中使用Redux有如下好处:         预测           始终有一个准确的数据源,就是store, 对于如何将actions以及应用的其他部分和当前的状态同步可以做到绝不混乱。         维护           具备可预测结果的性质和严格的组织结构让代码更容易维护。         组织           对代码应该如何组织更加严苛,这使代码更加一致,对团队协作更加容易。         测试          

multiple Redux-form with formValueSelector

北战南征 提交于 2020-01-06 14:20:13
问题 I'm trying to create multiple forms that have dependable dropdowns. Based on selection on dropdown1, some fields are shown and another dropdown is populated. To achieve the multiple forms, I have to pass a unique form key such as: panels.map(panel => <PanelForm key={panel.uuid} form={`PanelForm_${panel.uuid}`} /> ) However, to connect to state for changes, I have to use the redux formValueSelector which requires to set it to the form name passed which is dynamic and I don't know how to pass

Redux form validation runs after onChange handler function

≡放荡痞女 提交于 2020-01-06 08:44:22
问题 I have the following redux form: import React, { Component } from 'react'; import { connect } from 'react-redux'; import { Field, reduxForm, getFormSyncErrors } from 'redux-form'; import { createExpense } from '../../actions'; import { validateName, validateImage } from '../../helpers/expense_utils'; import { renderInputField, validate, renderTextAreaField, renderDropzoneField } from '../../helpers/form_utils'; const validators = [ { field: 'title', validator: validateName }, { field: