Can the strategy to store information from previous render be used in a custom hook?
Nessuno ha ancora preso questa issue.
- Lingua principale
- JavaScript
- Stelle
- 11.8k
- Fork
- 7.9k
- Merge medio
- 1g 11h
- PR unite (30g)
- 11
Descrizione
I was reading the strategy in the new beta docs, and was thinking whether it could be used in a custom hook? I tried doing it and it worked, but wanted to know whether it's advised or not.
To explain in code, my question is whether this is OK (same example from React docs but trend logic moved to another hook):
import { useState } from 'react';
const useCountTrend = (count) => {
const [prevCount, setPrevCount] = useState(count);
const [trend, setTrend] = useState(null);
if (prevCount !== count) {
setPrevCount(count);
setTrend(count > prevCount ? 'increasing' : 'decreasing');
}
return trend;
}
function CountLabel({ count }) {
const trend = useCountTrend(count);
return (
<>
<h1>{count}</h1>
{trend && <p>The count is {trend}</p>}
</>
);
}
function App() {
const [count, setCount] = useState(0);
return (
<>
<button onClick={() => setCount(count + 1)}>
Increment
</button>
<button onClick={() => setCount(count - 1)}>
Decrement
</button>
<CountLabel count={count} />
</>
);
}
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia dalla sezione collegata della documentazione beta di React sulla memorizzazione delle informazioni provenienti dai render precedenti e esamina l’esempio di custom-hook in questa issue. Determina se questo pattern è consigliato per i custom hooks e dove debba essere inserita questa indicazione nella documentazione. Il lavoro è completato quando la documentazione fornisce una risposta chiara supportata dall’esempio.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript, react
- Ambito
- documentation
- Tipo di issue
- Documentazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 25/100