繁体   English   中英

为什么redux State没有更新?

[英]Why is redux State no updating?

我有一个 Redux 操作,但我不能让它工作。 我需要找到嵌套到 firebase 集合数组中的 object 的索引。 从数据库中获取数据后,我计算了索引,但无法将其发送到 redux。 这可能是一个超级菜鸟 JavaScript 错误,我不知道为什么,但我无法更新我发送给 redux 的 object(reDatos)

export const calcularIdsAModificar = (MOid) => {
  let reDatos = {
    MOid: MOid,
    MOidindex: null,
  };

  return (dispatch, getState, { getFirebase, getFirestore }) => {
    const Firestore = getFirestore();
    const authorId = getState().firebase.auth.uid;
    let experienciasArray = 'sin datos';

    Firestore.collection('users')
      .doc(authorId)
      .get()
      .then((resp) => {
        experienciasArray = resp.data().experiencia;
        let newindex = experienciasArray.findIndex((expe) => expe.id === MOid);
        reDatos['MOidindex'] = newindex;
      })

      .then(dispatch({ type: 'ONCLICK_EDITAR_EXPERIENCIA', reDatos: reDatos }));
  };
};

这是问题所在:当 function 被声明时,第二个then立即调用。 要使其工作,您需要将处理程序(箭头 function 或函数声明)传递给then

这里要注意的另一件事是,您实际上并不需要第二个then 由于第一个then中的代码是同步的,因此您可以在其中调用调度。

 return (dispatch, getState, {getFirebase, getFirestore}) => {

       const Firestore = getFirestore();
       const authorId = getState().firebase.auth.uid;
       let experienciasArray = 'sin datos'
      
       

       Firestore.collection('users').doc(authorId).get().then(resp =>  {
           
        experienciasArray = resp.data().experiencia          
        let newindex = experienciasArray.findIndex(expe => expe.id === MOid)
        reDatos['MOidindex'] = newindex

        dispatch({ type:'ONCLICK_EDITAR_EXPERIENCIA', reDatos: reDatos })    

       });
      
        //.then(() =>  {dispatch...}); //Or call a dispatch inside an arrow function here.  

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM