r/javascript • • Jun 28 '18

The exact same app - 1 version made with React, Redux, and React-Router The other made with Vue, Vuex, Vue-Router. Good if you want to see both worlds. User Auth with JWT, note CRUD actions, Koa (Node) backend. MIT license.

https://johndatserakis.github.io/koa-react-notes-web/
459 Upvotes

98 comments sorted by

View all comments

Show parent comments

5

u/TheMr_ Jun 28 '18 edited Jun 28 '18

In regard to the return function, I see it as more separation of concerns. JSX is already being intermingled within each component alongside the normal JavaScript. For me, I'd say it helps keep the UI and Data separated more. What I found when learning React was that it tries to rely heavily on vanilla js where it can. Learning React taught me a lot more what could be done with vanilla js and has improved my overall understanding of just javascript as a whole.

And with that, it's fairly easy to loop over data within React just with plain javascript. Simply map over an array and pass in a component or element to be returned.

let state = {
    array: [1,2,3,4,5]
}

render(){
    return(
         <React.fragment>
             {this.state.array.map(number=><span>{number}</span>)}
         </React.fragment>
)}

Which will be rendered as the following into its parent container

<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>

You can also use the filter and map functions in conjunction to check whether an item should be rendered at all.

3

u/ImOutOfThisWorld Jun 28 '18

you forgot the "{}" in:

<React.fragment>
    {this.state.array.map(number=><span>{number}</span>)}
</React.fragment>

2

u/TheMr_ Jun 28 '18

Can't believe I forgot that. Thanks, I've edited my reply

3

u/crazyfreak316 Jun 28 '18

Learning React taught me a lot more what could be done with vanilla js and has improved my overall understanding of just javascript as a whole.

I can completely relate.

2

u/ihsw Jun 28 '18

<React.fragment> elements can be replaced with empty-bracket elements.

Eg:

<>
    {this.state.array.map(number=><span>{number}</span>)}
</>

And, furthermore, you can return bare arrays of rendered elements, so even that is unnecessary.

render() {
    return this.state.array.map(number=><span>{number}</span>);
}

1

u/NoInkling Jun 29 '18 edited Jun 29 '18

<React.fragment> elements can be replaced with empty-bracket elements.

Only with Babel 7, which is still in beta (and maybe Typescript?).

And, furthermore, you can return bare arrays of rendered elements, so even that is unnecessary.

You'll get a warning about missing keys though (which means it's less optimized).

Edit: actually the first example will also have the key issue, so you're right, there's no advantage to using the fragment in this case. Any time you use an array you should add keys though.

1

u/ihsw Jun 28 '18

<React.fragment> elements can be replaced with empty-bracket elements.

Eg:

<>
    {this.state.array.map(number=><span>{number}</span>)}
</>

1

u/Morphray Jun 30 '18

You forgot semicolons at the end of your statements. ;)