1

I have the following return in a react component:

render() {
     return(
            <div>
                <div className = "category-name">
                    {categories.map(category =>
                        <p>{category.name}</p>
                    )}
                </div>
                <div className = "category-name">
                    {categories.map(category =>
                        <Playlist categoryid={category.id}/>
                    )}
                </div>
            </div>
        );
}

This is displaying names of categories and then going through the array of names getting the description from the Playlist component. Could someone tell me how I would return the playlists under each name? I tried the following but it just won't work:

render() {
  return(
            <div>
                <div className = "category-name">
                    {categories.map(category =>{
                        return (<p>{category.name}</p>,
                            <Playlist categoryid={category.id}/>)
                        }     
                    )}
                </div>
            </div>
        );
}
3
  • So you only want to combine both map functions? I guess the second code example throws an error? Commented Apr 23, 2020 at 8:29
  • Are you getting any error in the second case? I think you may have to wrap the <p> tag and <Playlist> component within a single wrapper (<div>) if you are getting any error. Commented Apr 23, 2020 at 8:30
  • I wasn't getting any error even in the console. The name just wasn't displaying and it was going straight to the component. Commented Apr 23, 2020 at 8:33

2 Answers 2

6

You could used react fragments, using their full syntax <React.Fragment> allows you to put a key attribute within them :

<div className = "category-name">
    {categories.map(category => {
        return 
            <React.Fragment key={category.id}>
                <p>{category.name}</p>,
                <Playlist categoryid={category.id}/>
            </React.Fragment>
        }   
    )}
</div>

The <React.Fragment> element will not be rendered in the DOM and allows you to put any elements within them.

Sign up to request clarification or add additional context in comments.

5 Comments

<> </> That's the new thing I have learned from your code. Glad to learn that :)
<> is shorthand for <React.Fragment>, just in case you are wondering. reactjs.org/docs/fragments.html
Just adding, <></> is good for wrapping components though there is slight difference. React.Fragment also accepts key prop while <></>, Does not. React shows warning when you are rendering components inside loop without key prop.
Since my answer was lacking a lot of information and was not optimal yet somehow got attention, I updated it with the fragment syntax that allows keys.
After mocking about with the default "comma" syntax for an hour this saved my day! This works with returning several material-ui TableCell instances
2

You need to wrap your elements in map function in some html tag or React.Fragment:

render() {
  return(
            <div>
                <div className = "category-name">
                    {categories.map(category => (
                          <div key={category.id}>
                            <p>{category.name}</p>
                            <Playlist categoryid={category.id}/>
                         </div>
                        )
                    )}
                </div>
            </div>
        );
}

And please, don't forget to use key in your cycles

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.