r/reactjs • • Aug 01 '19

Beginner's Thread / Easy Questions (August 2019)

Previous two threads - July 2019 and June 2019.

Got questions about React or anything else in its ecosystem? Stuck making progress on your app? Ask away! We’re a friendly bunch.

No question is too simple. 🤔


🆘 Want Help with your Code? 🆘

  • Improve your chances by putting a minimal example to either JSFiddle or Code Sandbox. Describe what you want it to do, and things you've tried. Don't just post big blocks of code!

  • Pay it forward! Answer questions even if there is already an answer - multiple perspectives can be very helpful to beginners. Also there's no quicker way to learn than being wrong on the Internet.

Have a question regarding code / repository organization?

It's most likely answered within this tweet.


New to React?

Check out the sub's sidebar!

🆓 Here are great, free resources! 🆓


Any ideas/suggestions to improve this thread - feel free to comment here!


Finally, an ongoing thank you to all who post questions and those who answer them. We're a growing community and helping each other only strengthens it!

35 Upvotes

368 comments sorted by

View all comments

Show parent comment

3

u/Awnry_Abe Aug 30 '19

There are two things to iron out to understand when/why. The first is "when is the code JSX?" and "when is the code JS?" The second is, "When the code is JS, when do I use {} and when do I use ()?" Let's knock out the first, since it is a little easier to see. This won't be 100% correct, but should get you there.

Starting from the very first character of your .js file, you are in JS syntax mode until an opening tag is met, like <Pet ...>, at which time you enter JSX syntax mode. You'll stay there until until the end of the closing tag </Pet>. You can nest other <Components> and <markup> and stay in JSX mode. Now, you'll often need to "escape JSX-mode" and momentarily go back the the surrounding JS syntax and context to do things like assign properties. to do that, you enclose the JSX-wrapped JS in {}. So <Pet animal={pet.type} ...> hops into JS-landia to grap the animal property value. Your location= property does the same to evaluluate a literal tag. Literal tags are yet another fine example of parsing one context, escaping to JS via {}, and returning the string. Fun! Parens, (), are JS syntax, I believe. There are certain constraints on what can go on inside the {} of a <Component> tag, but let's not worry about those now. In between on <Component> tag and <TheNext>, such as right after your first <div>, you are free to do about anything JS wise. For instance, in your example, there is logic to determine "do we render a list of pets?" or "do we render a "no pets found" message? That's all JS, not JSX (except the JS emits more JSX--just apply recursive reasoning and start over at the beginning of this paragraph). TL;DR:

...JS...import React...js...<JSX...foo={...JS...}>..still jsx...{...JS...}...JSX...</JSX>...JS

The latter question, purely speaking in JS terms now, not JSX, is when should I use {} verses not? I believe, but I may be overlooking something, that this question only applies to ES6 "fat arrow" function implementations. However, doing things like returning and object {a:1, b:2} introduces another flavor of syntax for the beginner. Using just your example, you have 2 functions. One has an expression that is a return statement, one has an expression that is JSX. That the latter is JSX is not relevant. TL;DR

const foo = () => 42;

is identical to

const foo = () => { return 42; }

and

const foo = () => ({ a: 42 }); // but not this is not valid foo = () => { a: 42 }. Compare to first example.

is identical to

const foo = () => { return { a: 42 }; }

Which to use? I wouldn't reject either. In short, the {} keys will get a workout. We didn't even touch on object syntax of {...spread} and {destructuring}.

1

u/wallywally11 Aug 30 '19

Yes, this is very helpful. I think the biggest part I wasn’t seeing is that arrow function parens are just being used here to do an implicit return. I kept seeing them and thinking it was some weird syntax or context switching that got skipped over in the course. I guess there’s just a certain amount of assumed knowledge.

Unrelated, but this course is also doing a bunch of confit stuff with parcel and Babel, showing multiple ways to use different new syntaxes and I think all of that is really pulling away my learning focus. I’m going to finish the course, but I think I’ll head towards some of the more focused teaching in the future.

Thanks for your time. That definitely helped clear it up a lot!