How to access Redux state from getServerSideProps in NextJs?

Viewed 306

I'm just learning NextJs and how to use Redux with it, and I'm running into a problem. I have a store mapped to props, and using this method works fine for accessing the store from my main component:

const mapStateToProps = function(state) {
return {
    text: state.text,
    page: state.page,
    users: state.users
}}

function Github(props) {
return (
    <div>
      <h1>{props.page}</h1>
      <h1>{props.text}</h1>
    </div>
  );
}

However, I also have a getServerSideProps that I want to use the state data in:

 export async function getServerSideProps() { 
    const state = store.subscribe(()=>{
        const state = store.getState()
        return state
    })
    const res = await fetch(`https://api.github.com/search/users?=${state.text}&per_page=10&page=${`1`}`)
    const data = await res.json()
     return {
         props: { results: data, }
    }
 }

Currently I'm using store.subscribe, which doesn't work and doesn't pass the data to the getServerSideProps. I also tried passing the state down through props, like in the Github component above, but that also didn't work. Can anyone tell me what I'm doing wrong here?

0 Answers
Related