geolibra / react_redux Goto Github PK
View Code? Open in Web Editor NEWreact-redux demo
Home Page: https://luvjia.github.io/react_redux/dist/index.html
react-redux demo
Home Page: https://luvjia.github.io/react_redux/dist/index.html
Redux Thunk中间件可以让action创建函数先不返回action对象,而是返回一个函数,通过这个函数延迟dispatch或只在满足指定条件的情况下dispatch.
激活Redux Thunk中间件:在createStore中加入applyMiddleware(thunk)即可
bindActionCreators是redux提供的一个辅助方法,能够让我们以方法的形式来调用action。同时,自动dispatch对应的action。
function bindActionCreator(actionCreator, dispatch) { // 主要作用就是返回一个函数,当我们调用返回的这个函数的时候,就会自动的dispatch对应的action // 这一块其实可以更改成如下这种形式更好 // return function(...args) {return dispatch(actionCreator.apply(this, args))} return function() { return dispatch(actionCreator.apply(this, arguments)) } }
/** 参数说明: actionCreators: action create函数,可以是一个单函数,也可以是一个对象,这个对象的所有元素都是action create函数 dispatch: store.dispatch方法 */ export default function bindActionCreators(actionCreators, dispatch) { // 如果actionCreators是一个函数的话,就调用bindActionCreator方法对action create函数和dispatch进行绑定 if (typeof actionCreators === 'function') { return bindActionCreator(actionCreators, dispatch) } // actionCreators必须是函数或者对象中的一种,且不能是null if (typeof actionCreators !== 'object' || actionCreators === null) { throw new Error(
bindActionCreators expected an object or a function, instead received ${actionCreators === null ? 'null' : typeof actionCreators}. +
Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?`
)
}
// 获取所有action create函数的名字
const keys = Object.keys(actionCreators)
// 保存dispatch和action create函数进行绑定之后的集合
const boundActionCreators = {}
for (let i = 0; i < keys.length; i++) {
const key = keys[i]
const actionCreator = actionCreators[key]
// 排除值不是函数的action create
if (typeof actionCreator === 'function') {
// 进行绑定
boundActionCreators[key] = bindActionCreator(actionCreator, dispatch)
}
}
// 返回绑定之后的对象
/**
boundActionCreators的基本形式就是
{
actionCreator: function() {dispatch(actionCreator.apply(this, arguments))}
}
*/
return boundActionCreators
} 使用bindActionCreators之后
let { state, actions } = this.props;
actions={{eatApple: actions.eatApple }}
摘苹果, 之前的写法:
let { state, dispatch } = this.props
actions={{eatApple: id => dispatch(actions.eatApple(id))}}
<button onClick={() => dispatch(actions.pickApple())}>摘苹果`
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.