简体   繁体   English

如何在商店中设置初始状态

[英]How to set initial state in store

So i am trying to get state from the app store, and pass that into my component to render, one such problem im having is that it seems to not know the initial state of the specific state i am trying to use, for instance, if the state has already loaded (the action has dispatched and returned data from the API) then this works fine, however on a fresh load where the action has yet to dispatch it seems that the state is undefined, I've tried a few different things to set the state initially but it doesnt seemingly work, no matter what i do the state seems to be undefined at a fresh load.所以我试图从应用商店获取状态,并将其传递到我的组件中进行渲染,我遇到的一个这样的问题是它似乎不知道我尝试使用的特定状态的初始状态,例如,如果状态已经加载(动作已经分派并从 API 返回数据)然后这工作正常,但是在动作尚未分派的新加载上似乎状态未定义,我尝试了一些不同的事情最初设置状态但它似乎不起作用,无论我做什么,状态似乎在新加载时都未定义。 In my store i have this:在我的商店里,我有这个:

const votdFromStorage = localStorage.getItem("votd")
  ? JSON.parse(localStorage.getItem("votd"))
  : { votd: { bible: "", verse: [], passage: "", chapter: "" } };

const initialState = {
  // verse of the day
  votd: votdFromStorage,
};

This says to see if their already exists an item in localStorage and if it does return it, else return an object that has some inital values, the error im getting specifically is that it cant read bible of undefined.这表示要查看它们是否在 localStorage 中已经存在一个项目,如果它确实返回它,否则返回一个具有一些初始值的对象,我得到的具体错误是它无法读取未定义的bible And im receiving this error here in my home component我在我的家庭组件中收到这个错误

  const dispatch = useDispatch();
  const {
    votd: { bible, passage, verse, chapter },
    loading,
  } = useSelector((state) => state.votd);

  useEffect(() => {
    dispatch({ type: VOTD_REQUEST });
    dispatch(verseOfTheDay());
  }, [dispatch]);

Here I am using the useSelector hook to grab from the state the votd object.在这里,我使用useSelector钩子从状态中获取votd对象。 of which, it cannot find bible , I am assuming it'd also be unable to find the other values as well but bible is first.其中,它找不到bible ,我假设它也无法找到其他值,但bible是第一位的。 anyways, i believe the problem is how i am setting initial state of the votd object, i even tried to set the init state inside the reducer similar to the one you see in the store, but it still returns the same error.无论如何,我相信问题在于我如何设置 votd 对象的initial state ,我什至尝试在reducer类似于您在商店中看到的init状态,但它仍然返回相同的错误。

here is the reducer:这是减速器:

export const votdReducer = (state = {}, action) => {
  switch (action.type) {
    case VOTD_REQUEST:
      return { ...state, loading: true };
    case VOTD_SUCCESS:
      return { loading: false, votd: action.payload };
    case VOTD_FAIL:
      return { ...state, loading: false, error: action.payload };
    default:
      return state;
  }
};

also to help here is the initial state set to the store在这里也有帮助的是设置到store的初始状态

const initialState = {
  // verse of the day
  votd: votdFromStorage,
};

I think you need to put the initial vote in the initial reducer like what I did, you can get the idea.我认为您需要像我所做的那样将初始投票放在初始减速器中,您可以得到这个想法。 It just will solve the undefined problem.它只会解决未定义的问题。

const votdFromStorage = localStorage.getItem("votd")
  ? JSON.parse(localStorage.getItem("votd"))
  : {};    

const initialState = {
      // verse of the day
     votd: { bible: "", verse: [], passage: "", chapter: "" }
    }; 
  
    initialState.votd= {...initialState.votd,...votdFromStorage?.votd};

         export const votdReducer = (state = initialState , action) => {
          switch (action.type) {
            case VOTD_REQUEST:
              return { ...state, loading: true };
            case VOTD_SUCCESS:
              return { loading: false, votd: action.payload };
            case VOTD_FAIL:
              return { ...state, loading: false, error: action.payload };
            default:
              return state;
          }
        };

Edited...已编辑...

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

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