react-redux

2020-07-01  本文已影响0人  我爱阿桑
1 .安装redux
 npm install --save redux
2 .在store中新建index.js和reducer.js
import { createStore } from 'redux'  //  引入createStore方法
import reducer from './reducer'    
const store = createStore(reducer) // 创建数据存储仓库
export default store   //暴露出去   
const defaultState = {}  //默认数据
export default (state = defaultState,action)=>{  //就是一个方法函数
    return state
}
3.初始话store的数据
 const defaultState = {
    inputValue : 'Write Something',
    list:[
        '早上4点起床,锻炼身体',
        '中午下班游泳一小时'
    ]
}
export default (state = defaultState,action)=>{
    return state
}
4 .组件中获取store中的数据
 import store from './store'
 constructor(props){
    super(props)
    console.log(store.getState())
    this.state=store.getState();
}
5.更改store
 <input onChange={this.changeInputValue}
 constructor(props){
    super(props)
    this.state=store.getState();
    this.changeInputValue= this.changeInputValue.bind(this)
}
 changeInputValue(e){
    console.log(e.target.value)
    const action ={
        type:'change_input_value',
        value:e.target.value
    }
    store.dispatch(action)
}
6 .在store中接收

前面的课程说了store只是一个仓库,不具备管理能力,他会把接收到action自动转发给reducer,我们在reducer.js打印一下,看下结果

 export default (state = defaultState,action)=>{
    console.log(state,action)
    return state
}

-通过打印你可以知道,Reducer已经拿到了原来的数据和新传递过来的数据,现在要作的就是改变store里的值。我们先判断type是不是正确的,如果正确,我们需要从新声明一个变量newState。(记住:Reducer里只能接收state,不能改变state。),所以我们声明了一个新变量,然后再次用return返回回去。

Reducer.js

 export default (state = defaultState,action)=>{
    if(action.type === 'changeInput'){
        let newState = JSON.parse(JSON.stringify(state)) //深度拷贝state
        newState.inputValue = action.value
        return newState
    }
    return state
}
7.让组件发生变化

在组件中写入

  constructor(props){
    super(props)
    this.state=store.getState();
    this.changeInputValue= this.changeInputValue.bind(this)
    //----------关键代码-----------start
    this.storeChange = this.storeChange.bind(this)  //转变this指向
    store.subscribe(this.storeChange) //订阅Redux的状态
    //----------关键代码-----------end
}
  storeChange(){
     this.setState(store.getState())
 }
8. 更改store文件结构,让结构更加明确
 export const  CHANGE_INPUT = 'changeInput'
 import { CHANGE_INPUT  } from './store/actionTypes'
 changeInputValue(e){
    const action ={
        type:CHANGE_INPUT,
        value:e.target.value
    }
    store.dispatch(action)
}
 import {CHANGE_INPUT} from './actionTypes'
const defaultState = {
    inputValue : 'Write Something',
    list:[
        '早上4点起床,锻炼身体',
        '中午下班游泳一小时'
    ]
}
export default (state = defaultState,action)=>{
    if(action.type === CHANGE_INPUT){
        let newState = JSON.parse(JSON.stringify(state)) //深度拷贝state
        newState.inputValue = action.value
        return newState
    }
  
    return state
}
9.继续优化
 import {CHANGE_INPUT}  from './actionTypes'
// 再写上
export const changeInputAction = (value)=>({
    type:CHANGE_INPUT,
    value
})
import {changeInputAction} from './store/actionCreatores'

那那个方法也就可以变为

 changeInputValue(e){
        const action = changeInputAction(e.target.value)
        store.dispatch(action)
    }
// 这是最早的样子
//changeInputValue(e){
//   console.log(e.target.value)
//    const action ={
//        type:'change_input_value',
//        value:e.target.value
//    }
//   store.dispatch(action)
}
上一篇 下一篇

猜你喜欢

热点阅读