React redux 修改数据 store.dispatch(action)

一、获取数据store.getState()

二、修改数据 store.dispatch(action)
在这里插入图片描述
在这里插入图片描述
src/store/index.js

import { createStore } from 'redux'  //  引入方法
import reducer from './reducer'    
const store = createStore(reducer) // 创建数据存储仓库
export default store   

src/store/reducer.js

const defaultState = {
    num : 1
}
function fn (state = defaultState,action){
       state = JSON.parse(JSON.stringify(state)) //深度拷贝state
       switch (action.type) {
           case "NUM":
            state.num = action.num
               return state;
           default:
               return state;
       }
}
const state=fn
export default state

组件使用:
src/views/a.js

import React from 'react'
import store from '../store/index' 
class A extends React.Component {
constructor(props){
  super(props)
  this.state=store.getState();
  this.storeChange = this.storeChange.bind(this)  
  store.subscribe(this.storeChange)
}
changeInputValue(e){
  if(e.target.value){
    const action ={
      type:'NUM',
      num:JSON.parse(e.target.value)
    }
    store.dispatch(action)
  }else{
    const action ={
      type:'NUM',
      num:e.target.value
    }
    store.dispatch(action)
  }
}
storeChange(){
  this.setState(store.getState())
}
addClick(){
  var count=this.state.num +1
  const action ={
    type:'NUM',
    num:count
}
store.dispatch(action)
}
reduceClick(){
  var count=this.state.num -1
  const action ={
    type:'NUM',
    num:count
}
store.dispatch(action)
}
render() {
    return (
      <div >
          <p>
          <button onClick={this.reduceClick.bind(this)}>1</button>
          <input value={this.state.num} type="number" onChange={this.changeInputValue}/>
          <button onClick={this.addClick.bind(this)}>1</button>
          </p>
      </div >
    )
  }
}
export default A

补充:
1、bind(this) 解决:如果传递一个函数名给一个变量,然后通过在变量后加括号()来调用这个方法,此时方法内部的this的指向就会丢失。
2、主要用subscribe监听store,当store中数据发生变化就会更新数据 。
3、JSON.parse(JSON.stringify(state)) ,关于深度拷贝可去参考这位兄台的博客

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React中使用this.props.dispatch调用后端接口,也需要借助redux-thunk中间件来处理异步操作。下面是一个简单的例子: ```javascript // 定义一个异步action creator export const fetchData = () => { return async (dispatch) => { dispatch({ type: 'FETCH_DATA_REQUEST' }); try { const response = await fetch('/api/data'); const data = await response.json(); dispatch({ type: 'FETCH_DATA_SUCCESS', payload: data }); } catch (error) { dispatch({ type: 'FETCH_DATA_FAILURE', payload: error }); } }; } // 在组件中使用dispatch调用异步action creator import { connect } from 'react-redux'; import { fetchData } from './actions'; class MyComponent extends React.Component { componentDidMount() { // 调用异步action creator this.props.dispatch(fetchData()); } render() { // 根据store中的状态渲染组件 const { isLoading, data, error } = this.props; if (isLoading) { return <div>Loading...</div>; } else if (error) { return <div>Error: {error.message}</div>; } else { return ( <div> <ul> {data.map(item => ( <li key={item.id}>{item.name}</li> ))} </ul> </div> ); } } } const mapStateToProps = state => ({ isLoading: state.isLoading, data: state.data, error: state.error, }); export default connect(mapStateToProps)(MyComponent); ``` 在上面的例子中,fetchData是一个异步action creator,它返回一个函数,这个函数接收一个参数:dispatch。在函数内部,我们可以使用dispatch方法来分发多个同步action,以更新store中的状态。在组件中,我们通过connect函数将store中的状态映射为props属性,然后在componentDidMount中使用this.props.dispatch调用该方法,以触发异步操作。当异步操作完成后,根据store中的状态渲染组件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值