0

I have a return statement in JSX. I need to delete the first element of the array and return the reduced array and then map it. But the app crashes on the line of reducing array.

I checked - this is an array. So I suppose this is about that I write JSX incorrectly. Please, could you help me.

return (
    <div className="popup__toggle">
    {let newArr = event.slice(1)}                                                                                   
    {console.log(newArr)}                                                              
    {newArr.map((item, index) => {                                                                                                                                                                                                       
        return(                                                                                                         
            <div>{item.name}</div>                                                                                                      
        )
    )}
</div>

I need to delete the first index of the array and map it.

3 Answers 3

1

Notice 1:

You are handling JSX as a raw JS problem. You need to do it with functional solutions. So instead of defining variables using let you can slice the variable directly and then use the result.

So your code would be like below:

return (
    <div className="popup__toggle">                                                       
    {event.slice(1).map(item => {
        return(                                                                                                         
            <div>{item.name}</div>                                                                                                      
        )
    })}

</div>
)

Notice 2:

You didn't close your brace correctly. I mean the one in the map item. You haven't closed it. instead of that I recommend to return your result without brace and return. Like below:

{event.slice(1).map(item => item)}

This is as an abbreviation for that. So your could could be like below:

{event.slice(1).map(item => {
    return <div>{item.name}</div>
})}

Or

{event.slice(1).map(item => <div>{item.name}</div>)}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

return (
      <div className="popup__toggle">
        {event.slice(1).map(item => <div key={item.name}>{item.name}</div>)}                                                                                                                                                                                   
      </div>
     )

Comments

0

You almost got it right, but, instead of having your newArr var inside JSX, which exactly what you mentioned, do it outside your return function:

So I suppose this is about that I write JSX incorrectly.

Do this

render(){
  let newArr = event ? event.slice(1) : []; //the new place for the new array. also added a check [event &&] to check if the array is there or not. if its not there it will return an empty array to prevent rendering issues due to newArr.map().
  return (
    <div className="popup__toggle">
      {
        newArr.map((item, index) => <div key={`item.name-#${index + 1}`}>{item.name}</div>)
      }
    </div>
  )
}


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.