All language subtitles for 003 Using Stateful Lists_en

af Afrikaans
ak Akan
sq Albanian
am Amharic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

So how could we now update

this expenses array whenever a new expense is being added?

Well for this, we need to go to App.js

because that is where we have this expenses array.

There, we also have this addExpenseHandler function

which we added in the last course section

which has triggered whenever a new expenses added.

And we even received the expense as a parameter here.

So all we got to do is we have to add this expense

to this expenses array.

But of course, if we just edit like this

nothing would happen because you already learned

that just because you change some variable,

React won't update the component.

Instead to achieve that, you need to use state.

And therefore here in App.js,

where we manage our expenses,

we now wanna import useState from React

so that we can register some state here.

And I will actually grab this array here,

this constant and extract that out

of this component function and instead store it here

and name it Initial Expenses

or Dummy Expenses since that's what it is.

So that this actually is just some dummy data

which we can use to initialize our state with.

But then inside of the app component function

we call useState and I'll pass my dummy expenses

so this array full of dummy data

as an initial state value

so that we have some initial expenses to display.

But then here, we can again use de-structuring

as I showed it to you in the last section

to get access to our expenses

and to the set expenses state updating function.

And now that we got this here in the addExpenseHandler

where we wanna add a new expense,

we can call setExpenses to set our expenses array

to a new array which includes this new expense.

So how should we update this?

Well, we could pass a new array here

and then let's say we wanna add the new expense

as the first item in the array.

For that we could add the expense which we're getting here

as an item to this array and then use

the spread operator on the existing expenses

to pull out all the existing array elements

and populate the rest of this new array

with those existing elements.

We can use the spread operator not just on objects

but also on arrays.

And again, that's not a React feature.

That's a standard JavaScript feature.

However, if we would update the state like this

it would not really be correct,

as you learned in the last core section.

Instead, if you update your state

depending on the previous state

or to previous snapshot of this state,

you should use this special function form

for this state updating function.

So it would be passed a function as argument

to this state updating function

and that function will automatically receive

the latest state snapshot.

So here we would then get our previous expenses

automatically by React.

And we would return our new array

where we add this expense which we're getting

as a parameter here and where we then add

our previous expenses with that spread operator.

And that's the clean way of updating our state

when it's based on an older snapshot of that same state.

Now with that, we add our new expense.

And now since we're using our expenses state here

to pass it to expenses,

we should automatically have a dynamic list

which automatically updates when items are added.

So if we now add a book for $29.99 and pick some date here,

we see that being added here as a first item.

So this is working and now this is a truly dynamic list

by using state and by outputting these items

with this map method.

And that's how you output lists of data dynamically

with React.

Now let's have a look at this key warning

and what that is all about.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.