reactjs / reactjs/react.dev

Clarification about multiple contexts on "Scaling Up with Reducer and Context" documentation page

Aberta
#5,175 1 comentário 3 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

Linguagem predominante
JavaScript
Estrelas
11.8k
Forks
7.9k
Merge médio
1d 11h
PRs com merge (30d)
11

Descrição

Hi, new react docs are great. I had a question about the sample code in Scaling Up with Reducer and Context. I hope this is a reasonable way to provide feedback on a specific page. I did a quick issue search and didn't find anything related. Here is some of the sample code:

import { TasksContext, TasksDispatchContext } from './TasksContext.js';

export default function TaskApp() {
  const [tasks, dispatch] = useReducer(tasksReducer, initialTasks);
  // ...
  return (
    <TasksContext.Provider value={tasks}>
      <TasksDispatchContext.Provider value={dispatch}>
        ...
      </TasksDispatchContext.Provider>
    </TasksContext.Provider>
  );
}

It's interesting that you chose that style vs:


```js
import { TasksContext, TasksDispatchContext } from './TasksContext.js';

export default function TaskApp() {
  const [tasks, dispatch] = useReducer(tasksReducer, initialTasks);
  // ...
  return (
    <TasksContext.Provider value={{ tasks, dispatch }}>
        ...
    </TasksContext.Provider>
  );
}

If I understand correctly, the benefit of multiple contexts comes in when I've memoized a component that is a child of our contexts, and then I want to use dispatch inside that child component without subscribing to changes to tasks.

This seems either important to elaborate on in the documentation page, or if you don't want to make the page more complex, maybe it'd be simpler for everyone to just use one context? One potential problem with the sample code you've chosen to share, and without explanation, is that it suggests the use of a lot of contexts, one for every bit of data you might want to share, which can lead to overwrought code in my opinion.

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Direção de pesquisa

Comece pela página “Scaling Up with Reducer and Context” e pelo exemplo vinculado a ela, especialmente TaskApp e TasksContext.js. Compare os exemplos com contextos separados e com um contexto único e, em seguida, determine qual explicação ou orientação a página precisa para que os leitores entendam o trade-off e quando o exemplo é apropriado.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
javascript, react
Domínio
documentation
Tipo de issue
Documentação
Dificuldade
3/5
Tempo estimado
1-2 dias
Status de atividade
Estagnada
Clareza
Precisa de esclarecimento
Facilidade para iniciantes
35/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.