Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
809 views
in Technique[技术] by (71.8m points)

reactjs - NextJS: Redux store is returning empty when calling the store from another page either client-side or server-side

I am calling a list of data in a page on the server-side, which is going to redux store. I want this data to be available to other pages.

Now, I have another page where I want to edit an article on the client-side. I am fetching a random article (for testing only) from the previously populated redux store with useSelector(). But the store is returning empty for this page! How come the populated store is not available in this page?

For testing, I tried with wrapper.getServerSideProps() to render the page on the server-side by accessing the store on the server but it's returning empty also!

The page where redux store is populating with blog list:

export default function BlogsList({blogs}){
    return(
        <div className="mainCon">
            <ul>
                {blogs.map((item) => (
                    <li key={item.author}>
                        <span>{item.author}</span><br/>
                        <span>{item.title}</span>
                        <h4>{item.body_delta}</h4>
                    </li>
                ))}
            </ul>
        </div>
    );
}


export const getServerSideProps = wrapper.getServerSideProps( async ({store, req}) =>{
    store.dispatch({type: FETCH_BLOG_LIST });
    store.dispatch(END);
    await store.sagaTask.toPromise();

    const myStore = store.getState();
    const blogs = myStore.blogReducer.blog;  
    return { props: {blogs}, };
});

The store output: redux-devtools-screnshot

This is the client side only page where the store is returning empty:

export default function editBlog(){
    const myBlog = useSelector(state => state.blogReducer.blog);
    const body_delta = myBlog[1].body_delta;
    console.log(body_delta);    // Throws TypeError: Cannot read property 'body_delta' of undefined
                                // Since the store is empty here

    return (
        ...
    );
}

And this is it's empty store: redux-devtools screenshot empty-store

How do I access the store data from this page also?

Edit: I added getInitialProps in _app.js but still the store is empty!

class MyApp extends App {
  static getInitialProps = async ({Component, ctx}) => {

      return {
          pageProps: {
              ...(Component.getInitialProps ? await Component.getInitialProps(ctx) : {}),
          },
      };

  };

  render() {
      const {Component, pageProps} = this.props;

      return (
          <Component {...pageProps} />
      );
  }
}

export default wrapper.withRedux(MyApp);
question from:https://stackoverflow.com/questions/65849232/nextjs-redux-store-is-returning-empty-when-calling-the-store-from-another-page

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...