How do I access store state in React Redux?

后端 未结 5 838
小蘑菇
小蘑菇 2020-12-12 13:59

I am just making a simple app to learn async with redux. I have gotten everything working, now I just want to display the actual state onto the web-page. Now, how do I actua

5条回答
  •  时光说笑
    2020-12-12 14:41

    You want to do more than just getState. You want to react to changes in the store.

    If you aren't using react-redux, you can do this:

    function rerender() {
        const state = store.getState();
        render(
            
    { state.items.map((item) =>

    {item.title}

    )}
    , document.getElementById('app') ); } // subscribe to store store.subscribe(rerender); // do initial render rerender(); // dispatch more actions and view will update

    But better is to use react-redux. In this case you use the Provider like you mentioned, but then use connect to connect your component to the store.

提交回复
热议问题