首页 > 其他 > 详细

react-redux

时间:2020-05-07 18:15:06      阅读:43      评论:0      收藏:0      [点我收藏+]
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)
      }
}

react-redux

原文:https://www.cnblogs.com/cyany/p/12844268.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!