Redux-003-使用 react-redux

本文视频地址:https://www.qiuzhi99.com/movies/react-redux/150.html

参考链接
https://github.com/reactjs/react-redux
https://github.com/reactjs/react-redux/blob/master/docs/api.md

目录结构


image.png
  • actions定义action事件
  • constants定义常量
  • reducers定义处理的逻辑(传入旧的state和action返回新的state)

代码
index.js

import {createStore} from 'redux';
import reducer from './reducers/counter';
import {increment,decrement} from './actions’;

import {Provider} from 'react-redux’;

const store = createStore(reducer);

ReactDOM.render(
<Provider store={store}>
<App />
</Provider>
, document.getElementById('root'));

App.js
//Store传值的实现
import {connect} from 'react-redux’;

const mapStateToProps = (state) => {
console.log(state);
return {
counter: state
};
};

export default connect(mapStateToProps)(App);

  • index.js没有了事件的绑定和触发
  • 添加了Provider
  • 添加了connect
  • 添加了mapStateToProps
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容