All language subtitles for 002 JSX Limitations & Workarounds_en

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
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
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian Download
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:02,170 --> 00:00:05,320 In React we're working with JSX. 2 00:00:05,320 --> 00:00:07,930 We work with JSX all the time here 3 00:00:07,930 --> 00:00:09,420 in this course already. 4 00:00:09,420 --> 00:00:11,070 In this course project 5 00:00:11,070 --> 00:00:13,650 which we worked on in the last course module 6 00:00:13,650 --> 00:00:16,309 which you find attached to this lecture by the way 7 00:00:16,309 --> 00:00:18,160 in case you didn't follow along 8 00:00:18,160 --> 00:00:19,460 you can simply download it 9 00:00:19,460 --> 00:00:21,430 extract it, run NPM install 10 00:00:21,430 --> 00:00:23,840 and NPM start and you're good to go. 11 00:00:23,840 --> 00:00:28,080 In that project we of course used JSX. 12 00:00:28,080 --> 00:00:31,970 JSX is that code you return in your Components 13 00:00:31,970 --> 00:00:33,680 which in the end will be rendered 14 00:00:33,680 --> 00:00:35,840 to the real DOM by a React. 15 00:00:35,840 --> 00:00:37,560 That's what JSX is 16 00:00:37,560 --> 00:00:39,143 and we use it all the time. 17 00:00:40,470 --> 00:00:44,010 But JSX has certain limitations 18 00:00:44,010 --> 00:00:46,490 specifically there is one limitation 19 00:00:46,490 --> 00:00:49,470 which we all has already faced in this course. 20 00:00:49,470 --> 00:00:53,900 If we have adjacent root level JSX elements 21 00:00:53,900 --> 00:00:55,410 like in this example 22 00:00:55,410 --> 00:00:57,730 we'll get an error. 23 00:00:57,730 --> 00:00:59,840 And with root level 24 00:00:59,840 --> 00:01:04,300 I mean that we have two JSX elements next to Java 25 00:01:04,300 --> 00:01:07,580 not wrapped by another JSX element. 26 00:01:07,580 --> 00:01:11,570 And we then return these side by side JSX elements 27 00:01:11,570 --> 00:01:14,390 or we try to store them in variables. 28 00:01:14,390 --> 00:01:15,710 This will not work 29 00:01:15,710 --> 00:01:19,060 because in React in JSX in general 30 00:01:19,060 --> 00:01:22,850 you can't have more than one root JSX element. 31 00:01:22,850 --> 00:01:26,760 So if you return a value or if you store a value 32 00:01:26,760 --> 00:01:30,160 in a variable or in a constant or in a property 33 00:01:30,160 --> 00:01:35,160 that value must only be exactly one JSX element 34 00:01:36,130 --> 00:01:41,130 not two or three or four side by side adjacent elements. 35 00:01:41,370 --> 00:01:44,650 Now that one element which you are allowed to have 36 00:01:44,650 --> 00:01:47,360 may of course have more children 37 00:01:47,360 --> 00:01:50,860 which then also can be adjacent to each other. 38 00:01:50,860 --> 00:01:52,920 But that top most element 39 00:01:52,920 --> 00:01:54,640 which you're storing or returning 40 00:01:54,640 --> 00:01:57,200 must only be one element. 41 00:01:57,200 --> 00:01:58,170 And the reason for that 42 00:01:58,170 --> 00:02:01,320 is actually not that difficult to understand 43 00:02:01,320 --> 00:02:05,280 because this also isn't valid JavaScript. 44 00:02:05,280 --> 00:02:09,870 You can't return more than one thing in JavaScript either 45 00:02:09,870 --> 00:02:12,310 you can return one number, not two 46 00:02:12,310 --> 00:02:15,410 you can return one string, not two. 47 00:02:15,410 --> 00:02:17,630 Of course you could return an array 48 00:02:17,630 --> 00:02:19,980 and there you could have multiple numbers 49 00:02:19,980 --> 00:02:22,670 but an array is already a new value. 50 00:02:22,670 --> 00:02:24,150 An array is just the object 51 00:02:24,150 --> 00:02:26,530 so you still only return one array, 52 00:02:26,530 --> 00:02:30,378 one object, not two arrays at the same time. 53 00:02:30,378 --> 00:02:33,490 So that is really important to understand here 54 00:02:33,490 --> 00:02:38,010 your JSX code translates into React create element 55 00:02:38,010 --> 00:02:40,540 like you see it here at the bottom of the slide. 56 00:02:40,540 --> 00:02:44,170 And you must only have one React create element call 57 00:02:44,170 --> 00:02:45,580 which you return. 58 00:02:45,580 --> 00:02:48,180 Now any nested JSX elements 59 00:02:48,180 --> 00:02:51,100 so inside of that one root element 60 00:02:51,100 --> 00:02:52,560 could be side by side 61 00:02:52,560 --> 00:02:55,120 because into the React create element world 62 00:02:55,120 --> 00:02:57,880 those children of that root element 63 00:02:57,880 --> 00:03:02,150 would be the third and fifth and so on argument. 64 00:03:02,150 --> 00:03:03,950 And that's something I had a closer look 65 00:03:03,950 --> 00:03:05,900 at in the basics module 66 00:03:05,900 --> 00:03:09,020 where we explored how JSX translates into 67 00:03:09,020 --> 00:03:10,823 React create element calls. 68 00:03:11,920 --> 00:03:15,370 But because of that natural limitation 69 00:03:15,370 --> 00:03:20,370 of JavaScript we must only have one root JSX element. 70 00:03:20,760 --> 00:03:23,380 Now how can we work around that? 71 00:03:23,380 --> 00:03:24,970 There is one solution 72 00:03:24,970 --> 00:03:26,660 and actually we can already use 73 00:03:26,660 --> 00:03:28,030 that solution here in 74 00:03:28,030 --> 00:03:30,120 that add user Component in 75 00:03:30,120 --> 00:03:33,440 that provided project which you find attached. 76 00:03:33,440 --> 00:03:36,350 Here I do have a rapping div 77 00:03:36,350 --> 00:03:38,223 and that is a workaround. 78 00:03:39,180 --> 00:03:41,270 You can solve this problem 79 00:03:41,270 --> 00:03:46,270 by simply wrapping the adjacent elements with a div. 80 00:03:46,430 --> 00:03:47,340 If you do that 81 00:03:47,340 --> 00:03:48,960 if you wrap them with a div 82 00:03:48,960 --> 00:03:52,180 then you only have one thing that you return 83 00:03:52,180 --> 00:03:54,590 only one value. 84 00:03:54,590 --> 00:03:56,780 It's a bit like returning free numbers 85 00:03:56,780 --> 00:03:58,530 by putting them into an array. 86 00:03:58,530 --> 00:04:02,140 Here we're returning to nested JSX elements 87 00:04:02,140 --> 00:04:04,913 by putting them into one div which we return. 88 00:04:05,880 --> 00:04:07,790 Now this also doesn't have to be a div 89 00:04:07,790 --> 00:04:09,730 you could be using any element here 90 00:04:09,730 --> 00:04:12,100 you could also be using a custom Component here. 91 00:04:12,100 --> 00:04:13,660 The main thing that matters 92 00:04:13,660 --> 00:04:16,230 is that you only have one value 93 00:04:16,230 --> 00:04:19,113 which you return or which you store it in a constant. 94 00:04:20,769 --> 00:04:23,370 And that's all to what we're doing here. 95 00:04:23,370 --> 00:04:26,420 Now there're actually is always an alternative to that 96 00:04:26,420 --> 00:04:27,702 instead of using a div 97 00:04:27,702 --> 00:04:31,350 we could use a native JavaScript array. 98 00:04:31,350 --> 00:04:34,090 I could remove my opening and closing div tags 99 00:04:34,090 --> 00:04:37,160 and put my card Component here 100 00:04:37,160 --> 00:04:39,550 and that dynamic expression here 101 00:04:39,550 --> 00:04:43,100 into an array, separated by commas. 102 00:04:43,100 --> 00:04:46,280 I just would need to remove these curly braces here 103 00:04:46,280 --> 00:04:48,840 because we're no longer inside of JSX here 104 00:04:48,840 --> 00:04:50,910 we're inside of an array. 105 00:04:50,910 --> 00:04:52,670 That would be valid 106 00:04:52,670 --> 00:04:54,780 but this has a little gotcha. 107 00:04:54,780 --> 00:04:56,460 We can return an array 108 00:04:56,460 --> 00:05:01,150 because React is able to work with arrays of JSX elements. 109 00:05:01,150 --> 00:05:03,920 We did utilize that when we output our list 110 00:05:03,920 --> 00:05:05,000 of users here 111 00:05:05,000 --> 00:05:06,820 or also earlier in the course 112 00:05:06,820 --> 00:05:09,340 you learned about mapping a raise of data 113 00:05:09,340 --> 00:05:12,933 to JSX elements in the lists and conditionals module. 114 00:05:13,800 --> 00:05:15,480 So React knows how to work 115 00:05:15,480 --> 00:05:17,910 with a raise of JSX elements and hence 116 00:05:17,910 --> 00:05:18,743 of course here 117 00:05:18,743 --> 00:05:21,653 we could also return such an array of JSX elements. 118 00:05:22,800 --> 00:05:23,983 If we do that 119 00:05:23,983 --> 00:05:25,840 and we previewed application 120 00:05:25,840 --> 00:05:27,750 we do get a warning though 121 00:05:27,750 --> 00:05:30,720 because whenever you're working with an array 122 00:05:30,720 --> 00:05:32,250 of JSX elements 123 00:05:32,250 --> 00:05:34,960 React wants a key on every element. 124 00:05:34,960 --> 00:05:37,310 And that's no exception here 125 00:05:37,310 --> 00:05:38,480 it wants that key 126 00:05:38,480 --> 00:05:41,230 if you map dynamically through a list 127 00:05:41,230 --> 00:05:45,250 of data and map that data to JSX elements 128 00:05:45,250 --> 00:05:47,140 but it also wants such a key 129 00:05:47,140 --> 00:05:50,250 if you have a hard-coded array of JSX elements 130 00:05:50,250 --> 00:05:51,463 as we have it here. 131 00:05:52,310 --> 00:05:54,620 Now of course we can simply add a key 132 00:05:54,620 --> 00:05:56,730 and since this is not generated dynamically 133 00:05:56,730 --> 00:05:59,060 we can hard-code keys here 134 00:05:59,060 --> 00:06:01,280 error modal for example 135 00:06:01,280 --> 00:06:05,950 and add user a card. 136 00:06:05,950 --> 00:06:07,690 We could add the key prop 137 00:06:07,690 --> 00:06:09,986 with our own invented well use 138 00:06:09,986 --> 00:06:12,260 and we would be fine. 139 00:06:12,260 --> 00:06:14,570 And that would be perfectly fine 140 00:06:15,480 --> 00:06:18,010 but typically you don't use this solution 141 00:06:18,010 --> 00:06:19,620 because adding those keys 142 00:06:19,620 --> 00:06:21,680 wrapping it in an array 143 00:06:21,680 --> 00:06:23,240 is a little bit cumbersome. 144 00:06:23,240 --> 00:06:28,240 It's way easier to simply add a wrapping div here 145 00:06:29,080 --> 00:06:31,180 like we had it before. 146 00:06:31,180 --> 00:06:33,530 So I can just go back to that 147 00:06:33,530 --> 00:06:37,160 add my div here and now wrap this here back 148 00:06:37,160 --> 00:06:39,550 into curly braces 149 00:06:39,550 --> 00:06:41,100 and remove the comma here 150 00:06:41,100 --> 00:06:43,280 because now we have a dynamic expression 151 00:06:43,280 --> 00:06:44,750 instead of JSX again 152 00:06:44,750 --> 00:06:48,373 so we need the curly braces to evaluate this expression. 153 00:06:49,570 --> 00:06:51,130 So that's simply more convenient 154 00:06:51,130 --> 00:06:53,663 and therefore typically we use the wrapping div. 155 00:06:54,740 --> 00:06:56,690 However with the wrapping div 156 00:06:56,690 --> 00:06:59,340 or generally any wrapping element 157 00:06:59,340 --> 00:07:01,100 a new problem arises. 158 00:07:01,100 --> 00:07:05,010 Now we can end up with div soup. 159 00:07:05,010 --> 00:07:08,350 So we can end up with a real DOM that's being rendered 160 00:07:08,350 --> 00:07:11,130 where you have many nested React Components 161 00:07:11,130 --> 00:07:12,530 and all those Components 162 00:07:12,530 --> 00:07:15,320 for various reasons need wrapping divs 163 00:07:15,320 --> 00:07:17,180 or have wrapping Components. 164 00:07:17,180 --> 00:07:21,260 And you have all these unnecessary divs being rendered 165 00:07:21,260 --> 00:07:22,660 into real DOM 166 00:07:22,660 --> 00:07:24,890 even though they're only there 167 00:07:24,890 --> 00:07:29,323 because of this requirement or this limitation of JSX. 168 00:07:30,240 --> 00:07:33,000 And this is not an unrealistic scenario. 169 00:07:33,000 --> 00:07:35,020 In bigger applications 170 00:07:35,020 --> 00:07:40,020 It's a very possible that in your final HTML page 171 00:07:40,200 --> 00:07:42,660 which is being served to your end users 172 00:07:42,660 --> 00:07:46,390 you have a lot of unnecessary divs or other elements 173 00:07:46,390 --> 00:07:47,550 which are only there 174 00:07:47,550 --> 00:07:49,730 because you needed them as wrappers 175 00:07:49,730 --> 00:07:52,470 even though they don't add any semantic 176 00:07:52,470 --> 00:07:55,600 or structural meaning to your page. 177 00:07:55,600 --> 00:07:57,570 Now of course you don't have to care about that 178 00:07:57,570 --> 00:07:59,290 you might be fine with that 179 00:07:59,290 --> 00:08:01,770 but it could break your styling. 180 00:08:01,770 --> 00:08:03,920 If you have a wrapping div somewhere 181 00:08:03,920 --> 00:08:07,050 and you use nested CSS selectors 182 00:08:07,050 --> 00:08:09,760 that div could break stylings. 183 00:08:09,760 --> 00:08:12,190 And even if it doesn't break your styling 184 00:08:12,190 --> 00:08:14,570 it's still not a good practice. 185 00:08:14,570 --> 00:08:17,910 You're rendering too many HTML elements 186 00:08:17,910 --> 00:08:21,040 and ultimately that also makes your application slower 187 00:08:21,040 --> 00:08:22,290 because the browser needs 188 00:08:22,290 --> 00:08:24,020 to render all those elements 189 00:08:24,020 --> 00:08:26,610 and React needs to check all those elements 190 00:08:26,610 --> 00:08:28,390 or at least some of those elements 191 00:08:28,390 --> 00:08:31,050 if content needs to change. 192 00:08:31,050 --> 00:08:34,740 So rendering unnecessary content is generally 193 00:08:34,740 --> 00:08:37,400 never a good idea in programming. 194 00:08:37,400 --> 00:08:39,480 Hence this wrapping div 195 00:08:39,480 --> 00:08:43,320 or this wrapping element approach is okay 196 00:08:43,320 --> 00:08:44,453 but not ideal. 14479

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