redux

Error: This method is only meant to be run on single node. 0 found instead

无人久伴 提交于 2020-01-12 13:53:47
问题 I am testing a keybinding feature in a component. The component is rather simple, event listener for the keyup and fires up a redux action which will hide the component. I have cleaned up my code here to only relevant information. I am able to make the test pass if I just use the store dispatch to make the action call but that of course will defeat the purpose of this test. I am using Enzyme to simulate the keyup event with the appropriate event data (keycode for esc ) but I come across the

redux ,react-redux梳理,以及源码的学习

橙三吉。 提交于 2020-01-12 11:24:06
React Store:提供的方法{ store . dispatch () store . subscribe (() => { this . forceUpdate (); // this.setState({}); }); store . getState () } // import { createStore, applyMiddleware } from "redux"; import { createStore , applyMiddleware } from "../kredux" ; // import thunk from "redux-thunk"; // import logger from "redux-logger"; // 初始话,状态的修改 function counterReducer ( state = 0 , action ) { console . log ( "state" , state ); switch ( action . type ) { case "add" : return state + 1 ; case "minus" : return state - 1 ; default : return state ; } } // const store = createStore(counterReducer,

Add Redux in React project

回眸只為那壹抹淺笑 提交于 2020-01-12 10:58:18
问题 Adding redux in React project (Refactor a simple project with Redux) Consider a simple project, a counter application that works with two buttons, once for Increment and another for Decrement counter value. In an actual scenario, we use the state for holding counter value like this: in App.js : import React, {Component} from 'react'; import CounterApp from './CounterApp' class App extends Component { render() { return ( <CounterApp/> ); } } export default App; in CounterApp.js : import React,

Add Redux in React project

房东的猫 提交于 2020-01-12 10:53:48
问题 Adding redux in React project (Refactor a simple project with Redux) Consider a simple project, a counter application that works with two buttons, once for Increment and another for Decrement counter value. In an actual scenario, we use the state for holding counter value like this: in App.js : import React, {Component} from 'react'; import CounterApp from './CounterApp' class App extends Component { render() { return ( <CounterApp/> ); } } export default App; in CounterApp.js : import React,

React躬行记(12)——Redux中间件

↘锁芯ラ 提交于 2020-01-12 06:45:09
  Redux的中间件(Middleware)遵循了即插即用的设计思想,出现在Action到达Reducer之前(如图10所示)的位置。中间件是一个固定模式的独立函数,当把多个中间件像管道那样串联在一起时,前一个中间件不但能将其输出传给下一个中间件作为输入,还能中断整条管道。在引入中间件后,既能扩展Redux的功能,也能增强dispatch()函数,适应不同的业务需求,例如通过中间件记录日志、报告奔溃或处理异步请求等。 图10 中间件管道 一、开发模式   在设计中间件函数时,会遵循一个固定的模式,如下代码所示,使用了柯里化、高阶函数等函数式编程中的概念。 function middleware(store) { return function(next) { return function(action) { return next(action); }; }; }   middleware()函数接收一个Store实例,返回值是一个接收next参数的函数。其中next也是一个函数,用来将控制权转移给下一个中间件,从而实现了中间件之间的串联,它会返回一个处理Action对象的函数。由于闭包的作用,在这最内层的函数中,依然能调用外层的对象和函数,例如访问action所携带的数据、执行store中的dispatch()或getState()方法等。示例中的middleware(

How is state passed to selectors in a react-redux app?

自古美人都是妖i 提交于 2020-01-12 03:34:57
问题 I came across an example, where the mapStateToProps function is using memoization. I was just wondering how the "state" parameter is passed onto memoized selectors. After looking at the docs for reselect as well as redux, it seems that the mapStateToProps can return a function which accepts state as its argument, and the connect decorator might be the one passing the state to it but am not sure. Can someone please shed some light? views/tracklist/index.js const mapStateToProps =

How is state passed to selectors in a react-redux app?

馋奶兔 提交于 2020-01-12 03:34:09
问题 I came across an example, where the mapStateToProps function is using memoization. I was just wondering how the "state" parameter is passed onto memoized selectors. After looking at the docs for reselect as well as redux, it seems that the mapStateToProps can return a function which accepts state as its argument, and the connect decorator might be the one passing the state to it but am not sure. Can someone please shed some light? views/tracklist/index.js const mapStateToProps =

Redux:中间件

懵懂的女人 提交于 2020-01-11 15:57:05
redux中间件概念 比较容易理解。 在使用redux时,改变store state的一个固定套路是调用store.dispatch(action)方法,将action送到reducer中。 所谓中间件,就是在 dispatch发送action 和 action到达reducer 之间,加入一些中间层,对action进行处理。 在之前学习redux的异步操作时,用到的redux-thunk就是一个中间件。action抵达reducer之前先对其进行判断,如果是对象就直接送到reducer;如果是函数就执行。 中间件特点: 1.是一个独立的函数,与其他中间件没有依赖关系 2.中间件可以有多个,按顺序组合使用 3.有统一的接口 中间件说白了就是一些第三方的插件,用来增强dispatch的功能,让我们可以在action到达reducer之前对action作出判断、改造、log下信息、实现复杂逻辑等。 当我们使用了多个中间件时,action依次从前一个中间件传到下一个中间件,最后抵达reducer。也可能在某个中间件进行处理后,发现没必要传到reducer了,就不传给下一个中间件(相当于丢弃,不再去处理)。 中间件必须定义为一个函数,它的基本结构如下: 1 function middleWare({dispatch,getState}){ 2 return function(next){

Do I always need to return a value from a redux middleware?

僤鯓⒐⒋嵵緔 提交于 2020-01-11 00:57:08
问题 In the examples given in the redux docs, there always seems to be something being returned from middlewares. However, when I call next(action) and return nothing everything seems to work fine. In the redux source it appears to be calling dispatch on the returned value of every middleware. This leads me to believe that it provides an optional way to run dispatch after all the middlewares have run. Can someone confirm if we must ALWAYS return a value from a middleware, and if so why? 回答1: I

What “…” means in Javascript (ES6)? [duplicate]

两盒软妹~` 提交于 2020-01-10 19:50:49
问题 This question already has answers here : What do these three dots in React do? (23 answers) Closed 2 years ago . I am learning Redux, React and ES6. I already developed with JS, but this new world of ES6 are surprising me with a lot of new things like "=>" to declare arrow functions and other. However, in this new Redux studies, I confront with ... in middle of my code. Bellow I have an example: import { combineReducers, createStore } from 'redux' const userReducer = (state={}, action) => {