Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 在减速器中设置redux的初始状态_Reactjs_Redux_React Redux_Redux Thunk - Fatal编程技术网

Reactjs 在减速器中设置redux的初始状态

Reactjs 在减速器中设置redux的初始状态,reactjs,redux,react-redux,redux-thunk,Reactjs,Redux,React Redux,Redux Thunk,嘿,伙计们,我陷入了这样一种情况,我必须将reducer的初始状态设置为某个值,让我向你们展示代码 首先,我有一个这样的动作创作者 export const fetchuser = () => { return async dispatch => { const res = await axios.get("/api/currentuser"); dispatch({ type: "fetchuser", payload: res.data

嘿,伙计们,我陷入了这样一种情况,我必须将reducer的初始状态设置为某个值,让我向你们展示代码

首先,我有一个这样的动作创作者

export const fetchuser = () => {
  return async dispatch => {
    const res = await axios.get("/api/currentuser");
    dispatch({
      type: "fetchuser",
      payload: res.data
    });
  };
};
1.export default function(state = {}, action) {
   switch (action.type) {
      case "fetchuser":
        let data = action.payload||false;
        return {
          ...state,
          fetchuser: data       //any where you can access fetchuser data as well as previous state will not change.
        }
      default:
        return state;
   }
 }
它只是从api中获取数据并向reducer发送一个操作

export default function(state = {}, action) {
  switch (action.type) {
    case "fetchuser":
      return action.payload||false;
    default:
      return state;
  }
}
现在在第二个action creater中,我必须发出一个post请求并增加用户数据库中的“credits”值

export const handletoken = token => {
  return async dispatch => {
    const res = await axios.post("/api/stripe", token);
    dispatch({ type: "credits", payload: res.data });
  };
};
所以我在这里得到更新后的值,然后我把它传递给减速机

export default function(state = {}, action) {
  switch (action.type) {
    case "credits":
      return action.payload
    default:
      return state;
  }
}
然后将它们合并到reducer/index.js中

export default combineReducers({
auth: authreducer,
credits:creditsreducer
});
mapstatetoprops函数中app.js中auth reducer的控制台日志

 auth:
credits: 40
googleid: "109463598810933991924"
__v: 0
_id: "5d7fff2c4cb0604139055ce4"

在credits reducer中,正如你所见,我已经将状态的初始值定义为空对象,但我想将其设置为auth reducer的credits键的值,我可以很容易地将其设置为数组或硬编码的对象,但这里我需要将其值设置为已在另一个减速机中的值,那么如何实现这一点呢?

假设您需要等待
“fetchuser”
成功地在
credits减速机中设置积分,您可以处理
“fetchuser”
在您的
信用还原程序中执行操作
以及:

export default function(state = {}, action) {
  switch (action.type) {
    case "fetchuser":
      return action.payload ? action.payload.credits : state;
    case "credits":
      return action.payload
    default:
      return state;
  }
}

假设您需要等待
“fetchuser”
creditsreducer
中成功设置积分,那么您也可以在
creditsreducer
中处理
“fetchuser”
操作:

export default function(state = {}, action) {
  switch (action.type) {
    case "fetchuser":
      return action.payload ? action.payload.credits : state;
    case "credits":
      return action.payload
    default:
      return state;
  }
}

始终保持以前的减速器状态值。否则不使用redux状态值。像这样

export const fetchuser = () => {
  return async dispatch => {
    const res = await axios.get("/api/currentuser");
    dispatch({
      type: "fetchuser",
      payload: res.data
    });
  };
};
1.export default function(state = {}, action) {
   switch (action.type) {
      case "fetchuser":
        let data = action.payload||false;
        return {
          ...state,
          fetchuser: data       //any where you can access fetchuser data as well as previous state will not change.
        }
      default:
        return state;
   }
 }

按上述方式更改所有减速器。

始终保持以前的减速器状态值。否则不使用redux状态值。像这样

export const fetchuser = () => {
  return async dispatch => {
    const res = await axios.get("/api/currentuser");
    dispatch({
      type: "fetchuser",
      payload: res.data
    });
  };
};
1.export default function(state = {}, action) {
   switch (action.type) {
      case "fetchuser":
        let data = action.payload||false;
        return {
          ...state,
          fetchuser: data       //any where you can access fetchuser data as well as previous state will not change.
        }
      default:
        return state;
   }
 }

按上述方式更改所有减速器。

了解,但现在如何在另一个减速器中将初始状态设置为该状态?就像这里我如何访问fetchuser属性?好的,我明白了,但是现在在另一个reducer中我如何将初始状态设置为这个状态?像这里一样,我如何访问fetchuser属性?