npm install react-redux --save
npm install props-types --save
index.js
import { Provider } from "react-redux"
reactDOM.render((
<Provider store={store}>
<App />
</Provider>
))
app.js
import PropTypes from ‘props-types‘
import {connect} from ‘react-redux‘
static propTypes = {
count:PropTypes.number.isRequired,
increment:PropTypes.func.isRequired,
decrement:PropTypes.func.isRequired
}
increment = ()=>{
const number = 10;
this.props.increment(number);
}
render(){
const {count} = this.props.count;
return ()
}
导入改为:
export default connect(
state =>({count:state}),
{increment,decrment}
)(App)
redux默认不支持异步处理
redux-thunk这个依赖包可以解决
npm install --save react-redux
store.js
import {createStore,applyMiddleware} from ‘redux‘
import thunk from ‘redux-thunk‘
const store = createStore(
counter,
applyMiddleware(thunk)
)
export default store;
actions.js
同步返回的都是对象
异步返回的都是一个函数
export const incrementAsync = (number)=>{
return dispatch =>{
setTimeout(
dispatch(increment(number))
,1000)
}
}
原文:https://www.cnblogs.com/cyany/p/12844268.html