reactjs / reactjs/react.dev

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

Aperta
#5,175 1 commento 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
JavaScript
Stelle
11.8k
Fork
7.9k
Merge medio
1g 11h
PR unite (30g)
11

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dalla pagina “Scaling Up with Reducer and Context” e dall’esempio collegato, in particolare TaskApp e TasksContext.js. Confronta gli esempi con contesti separati e con un contesto singolo, quindi determina quale spiegazione o indicazione sia necessaria nella pagina affinché i lettori comprendano il compromesso e quando l’esempio sia appropriato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript, react
Ambito
documentation
Tipo di issue
Documentazione
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.