美文网首页
18.React-Redux的使用19-05-30

18.React-Redux的使用19-05-30

作者: 你坤儿姐 | 来源:发表于2019-05-30 18:04 被阅读0次

1.安装react-redux
yarn add react-redux
2.创建TodoList.js

import Reac, { Component } from 'react' ;
class  TodoList extends Component{
render() {
        return (
           <div>
        <div>
          <input />
          <button>提交</button>
        </div>
        <ul>
          <li>Dell</li>
        </ul>
      </div>
     )
   } }
export default TodoList;

3.创建store文件夹及index.js文件

import { createStore} from 'redux';
const store = createStore();
export default store;

4.创建reducer并在store中引入reducer
import reducer from './reducer'
reducer.js

const defaultState = {
  inputValue:'',
  list:[]
}
export default (state = defaultState, action) => {
    return state;
}

5.使用store里的数据
之前我们会在TodoList中引入store,并使用constructor获取store里的数据,如下

constructor(props) {
    super(props);
    this.state = store.getState();
}
<input value={this.state.inputValue} />

现在用react-redux写

到项目的index.js中使用Provider
import { Proviter } from 'react-redux' ;
import store from './store';
//定义一个App的组件
const App = (
//Proviter是react-redux提供的一个核心API
     <Provider store={store}>//Proviter提供器连接了store,那么Proviter里的所有组件都有能力获取store的内容了,所以TodoList就有能力获取store的内容了
          <TodoList />
     </Provider>
);
ReactDOM.render(APP, document.getElementById('root'));

6.到TodoList组件中使用react-redux里的connect方法获取store里的数据
import { connect }from 'react-redux' ;
让TodoList组件和store进行连接
export default connect (null, null)(TodoList) ;
从而衍生出连接方式如下

const mapStateToProps = (state) => {
    return {
       inputValue: state.inputValue //state里面的inputValue会映射到组件的props的inputValue里面去
   }
}
//让TodoList和store做连接,做连接就有规则,规则在mapStateToProps里
export default connect (mapStateToProps, null)(TodoList) ;

这时可以改写为<input value={this.props.inputValue} />
现在改input添加一个onChange方法

//把store的dispatch方法挂载到props上
const mapDispatchToProps = (dispatch) => {
     return {
}
}
export default connect(mapStateToProps,mapDispatchToProps)(Todolist);

所以就可以用this.props去掉用dispatch方法

<input
            value={this.props.inputValue}
            onChange={this.props.changeInputValue}
          />

changeInputValue函数指的就是mapStateToProps里的changeInputValue方法

const mapDispatchToProps = (dispatch) => {
  return {
    changeInputValue(e){
      const action = {
        type:'change_input_value',
        value:e.target.value
      }
       dispatch(action)//dispatch把action转发给reducer
    }
  }
}

Todolist完整代码:

import React, {Component} from 'react';
import {connect} from 'react-redux';

class  TodoList extends Component{
  render() {
    return(
      <div>
        <div>
          <input
            value={this.props.inputValue}
            onChange={this.props.changeInputValue}
          />
          <button>提交</button>
        </div>
        <ul>
          <li>Dell</li>
        </ul>
      </div>
    )
  }
}

const mapStateToProps = (state) => {
  return{
    inputValue:state.inputValue
  }
}
const mapDispatchToProps = (dispatch) => {
  return {
    changeInputValue(e){
      const action = {
        type:'change_input_value',
        value:e.target.value
      }
       dispatch(action)
    }
  }
}
export default connect(mapStateToProps,mapDispatchToProps)(TodoList);

7.reducer中处理action

export default (state = defaultState, action) => {
  if (action.type === 'change_input_value'){
    const newState = JSON.parse(JSON.stringify(state));
    newState.inputValue = action.value;
    return newState;
  }

相关文章

  • 18.React-Redux的使用19-05-30

    1.安装react-reduxyarn add react-redux2.创建TodoList.js 3.创建st...

  • 一个人在家

    猪猪君——成长经历19-05-30 这几天天气还算不错,打拳没有被晒伤。爸妈都去广州去了,老婆这几天也回娘家了,就...

  • 简书贝价格0.19继续震荡

    简书贝商人的日常——猪猪君 19-05-30 今天简书贝价格继续震荡,现在价格为0.19。介于现在简书贝价格十分不...

  • 【财商牛人行动营】:复盘才能翻盘19-05-30

    【0530复盘】 我要实现财富自由。 【每日财富行动】 (可以记录你今天做了什么) 1、阅读稻盛和夫的《六项精进》...

  • iconfont的使用(下载使用)

    1、下载文件 2、在生命周期中引入项目 beforeCreate () { var domModule = ...

  • Gson的使用--使用注解

    Gson为了简化序列化和反序列化的过程,提供了很多注解,这些注解大致分为三类,我们一一的介绍一下。 自定义字段的名...

  • 记录使用iframe的使用

    默认记录一下----可以说 这是我第一次使用iframe 之前都没有使用过; 使用方式: 自己开发就用了这几个属...

  • with的使用

    下面例子可以具体说明with如何工作: 运行代码,输出如下

  • this的使用

    什么是this? this是一个关键字,这个关键字总是返回一个对象;简单说,就是返回属性或方法“当前”所在的对象。...

  • this的使用

    JS中this调用有几种情况 一:纯粹的函数调用 这是函数的最通常用法,属于全局性调用,因此this就代表全局对象...

网友评论

      本文标题:18.React-Redux的使用19-05-30

      本文链接:https://www.haomeiwen.com/subject/axamtctx.html