Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
So, now, that we learned about useReducer
let me, again, sum up when you should use useState
and when it might be an idea to use useReducer.
Now, generally, you will know when you need useReducer.
That might not be the greatest rule of all times,
but you will see when you need useReducer,
for example, because using useState becomes too cumbersome,
you find yourself dealing with a lot of kind
of related state snapshots, which still are kind
of independent and you start updating them together
in a way that just doesn't work out.
Then, you want to consider using useReducer.
Now, to still give you a couple of ideas
when to use useState versus useReducer,
useState is your main state management tool.
Typically, you start with useState
and often it's all you need.
It's great for independent pieces of state and data.
It's great for simple state, you could say.
It's great if state updates are easy
and limited to a few kinds of updates.
So, if you don't have a lot of different cases
that will change a state and especially if you don't have,
let's say, an object as a state or anything like that.
Now, if you do have an object as a state
or a more complex state useReducer might be interesting
because, in general, useReducer is great
if you need more power and with more power,
I simply mean that you can write such a reducer function
that can contain more complex state updating logic
where you always are guaranteed to work
with the latest state snapshot.
And where you can move that potentially more complex logic
out of your component function body
into a separate reducer function.
So, all the restructure code, that might be interesting.
You should especially consider useReducer,
if you're dealing with related data
with state that is made up of related pieces of state.
So when you, for example, have a scenario,
as we had it with our form input state.
And, in general, useReducer can be helpful
if you have more complex state updates,
if you have different cases, different actions
that can change a state, cases like this.
Now, there is no hard rule and there, as always
in programming, is no clear wrong or right.
You can certainly also handle cases
where useReducer would be good with just useState,
especially when combining that with useEffect.
But sometimes using useReducer just might
be more elegant, or simpler.
On the other hand, you should absolutely
not always useReducer because often that will be overkill.
If you have just a simple state that just switches
between two different values for example,
using useReducer might be overkill.
And that's it for useReducer.
Now, you could absolutely also use useReducer
to manage the form state as a whole with useReducer,
instead of using useState for the form validity
you could replace these two reducers
and the form validity with one big reducer
that manages the entire form,
but that's something I will come back
to later in the forms module.
Since, for now, it's just important
that you understand useReducer and when to use it.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.