A page on the docs site to show how to create a hook that loads data form an external source such that there would be no gap
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
Let's say I want to create a hook called useSettings. This hook will load settings once from an API and cache the data.
This is a sample code:
import {
useEffect,
useState,
} from "react"
let cache
const useSettings = () => {
const [settings, setSettings] = useState(cache || [])
useEffect(() => {
if (cache) return
callApi("/settings")
.then(data => {
setSettings(data)
cache = data
})
}, [])
const getSetting = (key, fallback) => {
const setting = settings.find(i => i.key === key)
if (!settings) {
return fallback
}
return setting.value
}
return {
getSetting
}
}
export default useSettings
This works fine when the settings are loaded. But when a component uses this hook, there is an unpredictable time at the beginning when the settings are not loaded yet, and they get the fallback value.
I searched the docs and navigated the menu items. I could not find a page as a guide on how to remove that gap.
Please add a page for this purpose. Explain what is the react's official way to solve this problem. Explain how we can make sure that when we call a hook, we are guaranteed to get the data.
Guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Direção de pesquisa
Comece revisando a documentação existente do React e o exemplo de useSettings na issue para identificar onde devem ficar as orientações sobre o carregamento de dados externos. A página finalizada deve explicar a abordagem oficial para evitar a ausência inicial de dados e como um hook pode garantir a disponibilidade dos dados antes que os consumidores os utilizem.
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
- 4/5
- Tempo estimado
- 3-5 dias
- Status de atividade
- Estagnada
- Clareza
- Razoavelmente clara
- Facilidade para iniciantes
- 42/100