老莫的笔记本  
  
查看: 757|回复: 0

React Redux-saga 与 react-dedux

[复制链接]

662

主题

878

帖子

5139

积分

超级版主

Rank: 8Rank: 8

积分
5139
发表于 2020-9-9 16:02:56 | 显示全部楼层 |阅读模式
Redux-saga 中间件的使用 [异步处理 数据 ,和  redux-thunk 的 意义一样 ,
                中间件是redux 的中间件 主要是处理action 和 store 之间的关系]
               
                react-redux
               
                安装“react-redux”
                npm install react-redux   
               
               
                react-redux 的核心API

                index.js【最主要的那个】 代码:
               

  1.                                 import React feom 'react';
  2.                                 import ReactDom from 'react-dom';
  3.                                 import TodoList from './TodoList';
  4.                                
  5.                                 //这是react-redux核心API 在此模块内的子组件,如果 Provider与stor 做了关联,
  6.                                 // 则此模块内的模块都能调用 store
  7.                                 import { Provider } from 'react-redux';
  8.                                 import store from './store';
  9.                                
  10.                                 const App = (
  11.                                         // 就是这要做关联的
  12.                                         <Provider store ={store}>
  13.                                                 <TodoList></TodoList>
  14.                                         </Provider>
  15.                                 );
  16.                                
  17.                                
  18.                                 ReactDom.render(APP,document.getElementByTd('root));
  19.                        
复制代码



                        Todolist.js组件代码
               

  1.                        
  2.                                 import React feom 'react';
  3.                                 import store from './store';
  4.                                 // 这是在react-redux 用于接收 Provider 中store 的API
  5.                                 import { connect } from 'react-redux';
  6.                                
  7.                                
  8.                                 class TodoList extends Component{
  9.                                         // 以前在 constructor(props) 中接收store ,用了react-redux现在不用

  10.                                         render(){
  11.                                                 return (
  12.                                                         <div>
  13.                                                                 <div>
  14.                                                                         <input value={this.props.inputValue}/>
  15.                                                                         <button>提交</button>
  16.                                                                 </div>
  17.                                                                 <ul>
  18.                                                                         <li>哈哈哈</li>
  19.                                                                 </ul>
  20.                                                         </div>
  21.                                                 )
  22.                                         }
  23.                                 }
  24.                                
  25.                                
  26.                                 // 链接的方式 [意思:  把store 中的组件 映射到 props上]
  27.                                 // 所以在render 中,来自store 的数据 就得写成 this.props.inputValue
  28.                                 const mapStateToProps = (state)=>{
  29.                                         return {
  30.                                        
  31.                                                 inputValue :state.inputValue       
  32.                                         }
  33.                                 }
  34.                                
  35.                                 // 在搞一个处理 函数,是处理store中的数据的函数
  36.                                 const mapDispatchToProps = (dispatch) ={
  37.                                         return {
  38.                                                 changeInputValue(e)
  39.                                                 {
  40.                                                         const action = {
  41.                                                                 type:'change_input_value',
  42.                                                                 value: e.target.value
  43.                                                         }
  44.                                                         dispatch(action);
  45.                                                 }
  46.                                                
  47.                                         }
  48.                                 }
  49.                                
  50.                                
  51.                                 // 重点在这 connect链接 [参数1 链接方式, 参数2 处理 store 内数据的函数]
  52.                                 export default connect(mapStateToProps,mapDispatchToProps)(TodoList);
  53.                                
  54.                        
复制代码

                       

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表