redux学习笔记2:redux和react-redux

前端之家收集整理的这篇文章主要介绍了redux学习笔记2:redux和react-redux前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.我们梳理一下react和redux如何配合工作的

1.redux提供createStore方法创建创建状态数store

2.react-redux提供Provider组件把store注册react的所有组件中

3.利用reduxcombineReducers可以合并设置各个状态和对应的逻辑处理,给createStore方法使用

4.创建各个状态和逻辑,给3使用

5.创建组件,利用react-reduxconnect封装组件可以把store的状态dispatch传给组件,在组件的this.props获取

6.组件中this.props.xx获取store的状态和dispatch,显示调用(acton)

2.一个react+redux+react-redux的简单示例

index.html

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <Meta charset="utf-8">
  5. <Meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
  6. <Meta name="theme-color" content="#000000">
  7. <title></title>
  8. </head>
  9. <body>
  10. <div id="root"></div>
  11. </body>
  12. </html>

src目录结构:

index.js

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3.  
  4. import registerServiceWorker from './registerServiceWorker';
  5. //中间件 dispatch支持函数action
  6. import thunkMiddleware from 'redux-thunk'
  7.  
  8. //redux 和react-redux(关联react和redux)
  9. import { createStore,applyMiddleware } from 'redux';
  10. import { Provider } from 'react-redux';
  11.  
  12.  
  13. //reducers 状态树state和逻辑操作
  14. import indexRedux from './indexRedux.js'
  15.  
  16. //app页面
  17. import App from './App.js'
  18. //创建状态树和设置
  19.  
  20. //使用中间件
  21. const createStoreWithMiddleware = applyMiddleware(thunkMiddleware)(createStore);
  22.  
  23. //生成状态树对象
  24. const store = createStore(indexRedux);
  25.  
  26.  
  27. //start 状态树应用到全局 通过Provider
  28. ReactDOM.render(<Provider store={store}><App /></Provider>,document.getElementById('root'));
  29. registerServiceWorker();

App.js

  1. import React,{ Component } from 'react';
  2.  
  3. //=====引入组件=====
  4.  
  5. import Sub1ReactRedux from './page/Sub1ReactRedux.js'
  6. import Sub2ReactRedux from './page/Sub2ReactRedux.js'
  7. import Sub3ReactRedux from './page/Sub3ReactRedux.js'
  8. import Sub4ReactRedux from './page/Sub4ReactRedux.js'
  9.  
  10. //=====组件=====
  11.  
  12. class App extends Component {
  13.  
  14. render() {
  15. return (
  16. <div>
  17. <Sub1ReactRedux/>
  18. <Sub2ReactRedux/>
  19. <Sub3ReactRedux/>
  20. <Sub4ReactRedux/>
  21. </div>
  22. );
  23. }
  24. //生命周期函数,渲染完毕
  25. componentDidMount() {
  26. console.log("app渲染完毕,项目开始了")
  27. }
  28. }
  29.  
  30.  
  31. export default App

indexRedux.js

  1. import { combineReducers } from 'redux';
  2.  
  3. //子reducer
  4. import list from './page/Sub1Redux.js'
  5. import hello from './page/Sub2Redux.js'
  6. import ajaxinfo from './page/Sub3Redux.js'
  7. import middleware from './page/Sub4Redux.js'
  8.  
  9. //合并reducer
  10. var indexRedux = combineReducers({
  11. list,hello,ajaxinfo,middleware
  12. })
  13.  
  14. export default indexRedux

page目录结构:

Sub1.js

  1. import React,{ Component } from 'react';
  2.  
  3. //=====组件=====
  4.  
  5. class Sub1 extends Component {
  6. //添加一条数据
  7. addone(){
  8. var newval=document.getElementById("additem").value;
  9. this.props.ADDTODO(newval);
  10. };
  11. render() {
  12. return (
  13. <div>
  14. <div>{this.props.name}</div>
  15. <ul>
  16. {
  17. this.props.list.map(function(v,i){
  18. return <li key={v.id}>{v.name}</li>
  19. })
  20. }
  21. </ul>
  22. <input type="text" id="additem" />
  23. <button onClick={this.addone.bind(this)}>添加一条</button>
  24. <hr />
  25. </div>
  26. );
  27. }
  28. componentDidMount() {
  29. console.log("sub1渲染完毕")
  30. }
  31.  
  32. }
  33.  
  34.  
  35.  
  36. export default Sub1

Sub1ReactRedux.js

  1. import { connect } from 'react-redux';
  2.  
  3. //=====引入组件=====
  4. import Sub1 from './Sub1.js'
  5.  
  6.  
  7. //=====react-redux 封装组件=====
  8.  
  9. // 哪些 Redux 全局的 state 是我们组件想要通过 props 获取的?
  10. function mapStateToProps(state) {
  11. return {
  12. list: state.list
  13. };
  14. }
  15.  
  16. // 哪些 action 创建函数是我们想要通过 props 获取的?
  17. function mapDispatchToProps(dispatch) {
  18. return {
  19. ADDTODO:function(newval){
  20. dispatch({type:"ADD_TODO",name:newval})
  21. }
  22. };
  23. }
  24.  
  25. //封装传递state和dispatch
  26. var Sub1ReactRedux = connect(mapStateToProps,mapDispatchToProps)(Sub1);
  27.  
  28. export default Sub1ReactRedux

Sub1Redux.js

  1. //reducer
  2.  
  3. var listjson=[
  4. {id:1,name:"d1111"},{id:2,name:"d222"},{id:3,name:"d333333"},{id:4,name:"d44444"}
  5. ];
  6.  
  7. function list(state = listjson,action) {
  8. switch (action.type) {
  9. case "ADD_TODO":
  10. //获取最后一个数据的id然后+1
  11. var newid=Number(state[state.length-1].id)+1;
  12. return [
  13. ...state,{name: action.name,id: newid}
  14. ]
  15. default:
  16. return state
  17. }
  18. }
  19.  
  20.  
  21. export default list

Sub2.js

  1. import React,{ Component } from 'react';
  2.  
  3. //=====组件=====
  4.  
  5. class Sub2 extends Component {
  6. render() {
  7. return (
  8. <div>
  9. <h3>{this.props.hello}</h3>
  10. <ul>
  11. {
  12. this.props.list.map(function(v,i){
  13. return <li key={v.id}>{v.name}</li>
  14. })
  15. }
  16. </ul>
  17. <hr />
  18. </div>
  19. );
  20. }
  21. componentDidMount() {
  22. console.log("sub2渲染完毕")
  23. }
  24. }
  25.  
  26.  
  27. export default Sub2

Sub2ReactRedux.js

  1. import { connect } from 'react-redux';
  2.  
  3. //=====引入组件=====
  4. import Sub2 from './Sub2.js'
  5.  
  6.  
  7. //=====react-redux 封装组件=====
  8.  
  9. // 哪些 Redux 全局的 state 是我们组件想要通过 props 获取的?
  10. function mapStateToProps(state) {
  11. return {
  12. hello:state.hello,list: state.list
  13. };
  14. }
  15.  
  16. // 哪些 action 创建函数是我们想要通过 props 获取的?
  17. function mapDispatchToProps(dispatch) {
  18. }
  19.  
  20. //封装传递state和dispatch
  21. var Sub2ReactRedux = connect(mapStateToProps)(Sub2);
  22.  
  23. export default Sub2ReactRedux

Sub2Redux.js

  1. //reducer
  2.  
  3. var wh="hello";
  4.  
  5. function hello(state = wh,action) {
  6. switch (action.type) {
  7. case "A":
  8. return "hello"
  9. default:
  10. return state
  11. }
  12. }
  13.  
  14. export default hello

Sub3.js

  1. import React,{ Component } from 'react';
  2.  
  3. //=====组件=====
  4.  
  5. class Sub3 extends Component {
  6. f5(){
  7. this.props.RENAME(this.props.ajaxinfo.name+1)
  8. }
  9. res(){
  10. this.props.RESNAME()
  11. }
  12. render() {
  13. return (
  14. <div>
  15. 显示用户数据
  16. <div>{this.props.ajaxinfo.name}</div>
  17. <div>{this.props.ajaxinfo.sex}</div>
  18. <div>ajax数据{this.props.ajaxinfo.more}</div>
  19. <button onClick={this.f5.bind(this)}>姓名+1</button>
  20. <button onClick={this.res.bind(this)}>姓名重置</button>
  21. <hr />
  22. </div>
  23. );
  24. }
  25. componentDidMount() {
  26. console.log("sub3渲染完毕")
  27. //alert(this.props.ajaxinfo.name)
  28. this.props.STARTGETMORE();
  29. setTimeout(function(){
  30. this.props.SUCCESSGETMORE("我是ajax数据")
  31. }.bind(this),2000)
  32. }
  33. }
  34.  
  35.  
  36. export default Sub3

Sub3ReactRedux.js

  1. import { connect } from 'react-redux';
  2.  
  3. //=====引入组件=====
  4. import Sub3 from './Sub3.js'
  5.  
  6.  
  7. //=====react-redux 封装组件=====
  8.  
  9. // 哪些 Redux 全局的 state 是我们组件想要通过 props 获取的?
  10. function mapStateToProps(state) {
  11. return {
  12. ajaxinfo: state.ajaxinfo
  13. };
  14. }
  15.  
  16. // 哪些 action 创建函数是我们想要通过 props 获取的?
  17. function mapDispatchToProps(dispatch) {
  18. return {
  19. RENAME:function(name){
  20. dispatch({type:"RE_NAME",name:name})
  21. },RESNAME:function(){
  22. dispatch({type:"RES_NAME",name:"小李子"})
  23. },STARTGETMORE:function(){
  24. dispatch({type:"START_GET_MORE",more:"start"})
  25. },SUCCESSGETMORE:function(more){
  26. dispatch({type:"RES_NAME",name:more})
  27. }
  28. };
  29. }
  30.  
  31. //封装传递state和dispatch
  32. var Sub3ReactRedux = connect(mapStateToProps,mapDispatchToProps)(Sub3);
  33.  
  34. export default Sub3ReactRedux

Sub3Redux.js

  1. //reducer
  2.  
  3. var ajaxinfoinit={name:"小李子",sex:"男",isFetching: false,more:""};
  4.  
  5. function ajaxinfo(state = ajaxinfoinit,action) {
  6. switch (action.type) {
  7. case "RE_NAME":
  8. //修改姓名
  9. return {...state,name:action.name}
  10. case "RES_NAME":
  11. //重设
  12. return {...state,name:action.name}
  13. case "START_GET_MORE":
  14. //AJAX开始请求更多信息
  15. return {...state,more:action.more}
  16. case "SUCCESS_GET_MORE":
  17. //请求成功返回更多信息
  18. return {...state,more:action.more}
  19. case "ERROR_GET_MORE":
  20. //请求失败
  21. return {...state,error:action.error}
  22. default:
  23. return state
  24. }
  25. }
  26.  
  27. export default ajaxinfo

Sub4.js

  1. import React,{ Component } from 'react';
  2.  
  3. //=====组件=====
  4.  
  5. class Sub4 extends Component {
  6. getmore(){
  7. var info=[
  8. {val:"第n条数据"},{val:"第n+1条数据"},{val:"第n+2条数据"}
  9. ];
  10. this.props.POSTMD(info);
  11. }
  12. removedata(){
  13. this.props.REMOVEMD();
  14. }
  15. render() {
  16. return (
  17. <div>
  18. 数据处理
  19. <div style={{display:this.props.middleware.list.length<=0?"block":"none"}}>沒有数据</div>
  20. <ul style={{display:this.props.middleware.list.length<=0?"none":"block"}}>
  21. {
  22. this.props.middleware.list.map(function(item,i){
  23. return <li key={i}>{item.val}</li>
  24. })
  25. }
  26. </ul>
  27. <button onClick={this.getmore.bind(this)}>加载更多(一次模拟从后台拿到3条)</button>
  28. <button onClick={this.removedata.bind(this)}>移除所有数据</button>
  29. <hr />
  30. </div>
  31. );
  32. }
  33. componentDidMount() {
  34. console.log("sub4渲染完毕")
  35. }
  36. }
  37.  
  38.  
  39. export default Sub4

Sub4ReactRedux.js

  1. import { connect } from 'react-redux';
  2.  
  3. //=====引入组件=====
  4. import Sub4 from './Sub4.js'
  5.  
  6.  
  7. //=====react-redux 封装组件=====
  8.  
  9. // 哪些 Redux 全局的 state 是我们组件想要通过 props 获取的?
  10. function mapStateToProps(state) {
  11. return {
  12. middleware: state.middleware
  13. };
  14. }
  15.  
  16. // 哪些 action 创建函数是我们想要通过 props 获取的?
  17. function mapDispatchToProps(dispatch) {
  18. return {
  19. POSTMD:function(info){
  20. dispatch({type:"START_MD"});
  21. //ajax success数据
  22. setTimeout(function(){
  23. dispatch({type:"SUCCESS_MD",info:info})
  24. },2000);
  25. },REMOVEMD:function(){
  26. dispatch({type:"REMOVE_MD",msg:""})
  27. }
  28. };
  29. }
  30.  
  31. //封装传递state和dispatch
  32. var Sub4ReactRedux = connect(mapStateToProps,mapDispatchToProps)(Sub4);
  33.  
  34. export default Sub4ReactRedux

Sub4Redux.js

  1. //reducer
  2.  
  3. var middlewareinit={
  4. list:[
  5. {val:"第1条数据"},{val:"第2条数据"},{val:"第3条数据"}
  6. ]
  7. };
  8.  
  9. function middleware(state = middlewareinit,action) {
  10. switch (action.type) {
  11. case "START_MD":
  12. //请求成功返回更多信息
  13. return {list:[...state.list]}
  14. case "SUCCESS_MD":
  15. //请求成功返回更多信息
  16. return {list:[...state.list,...action.info]}
  17. case "REMOVE_MD":
  18. //请求失败
  19. return {list:[]}
  20. default:
  21. return state
  22. }
  23. }
  24.  
  25. export default middleware

猜你在找的React相关文章