Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

179
Visualizações
How to use redux for data fetching in Next.js

So previously I migrated my code to use Redux in my Next.js project. Althought it made it easier to scale, I do still have some issues with it.

Basically I used to fetch data from the getServerSideProps server in the previous version. But it becomes more complex when I think about fetching from a redux action.

BTW, I am using redux-thunk.

Here is my code:

action.js

export const FetchPosts = () => async (dispatch: Dispatch) => {
    const { data } = await api.fetchPosts();
    dispatch({ type: FETCH_ALL, payload: data });
}

reducer.js

export default (state: any = [], action: AnyAction) => {
    switch (action.type) {
        case FETCH_ALL:
            return [...state, action.payload.data]
        default:
            return state;
    }
}

component.tsx

const Component:FC = () => {
    
    const dispatch = useDispatch();
    const posts = useSelector(state => state.posts);

    useEffect(() => {
      // Should i fetch in here?
      dispatch(getPosts());
    }, []);

    return (
        <div>
          {/* ... */}
        </div>
    );
}

export async function getServerSideProps() { 

  // Should i even fetch here?

  return {
    props: { posts }
  }
}

Can anyone help?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

If the data that you are fetching important for SEO purpose you should be fetching data in getServerSideProps. So, when the server ships the HTML to the client side, your html will be populated with the data (imagine you have an ecommerce store and you want to index your shopping item ). BUt to use the serverside redux, you should be implementing next-redux-wrapper

If the data is not important for SEO, for example, admin needs to view the user data, you can fetch the data on the client-side.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda