0

I have a problem in mapping an array in React, Here is the problem : this is my state of the parent element :

this.state = {
  grouping: 3,
  bars: [{
    snare: Array(16).fill('0'),
    hihat: Array(16).fill('0'),
    kick: Array(16).fill('0'),
  }]
}

i wanna pass the bars to the child element same this :

<Bar bar={this.state.bars}>

and in 'Bar' class i write this code :

class Bar extends Component {
    render(){
        const Bars = this.props.bars.map((bar)=>{
            return('something')
        }
        return({Bars})
    }
}

after this codes this error occurs to me :

Objects are not valid as a React child (found: object with keys {Bars}). If you meant to render a collection of children, use an array instead.

help me, please

5
  • 1
    Why are you returning an object instead of Bars array? Commented Jun 3, 2018 at 21:02
  • @devserkan where ? Commented Jun 3, 2018 at 21:03
  • return({Bars}) so, {} makes it an object. Commented Jun 3, 2018 at 21:04
  • 1
    or something like return (<div>Bars</div>) instead Commented Jun 3, 2018 at 21:06
  • Yes you are skipping JSX in your return as @MichaelSorensen pointed. Though, there should be curly braces this time :) Commented Jun 3, 2018 at 21:10

1 Answer 1

5

Your issue is that a React component should only return a null or JSX.

you should be able to get what you want by doing the following:

class Bar extends Component {
    render(){
        return(
            {
                this.props.bars.map((bar)=>{
                    return <div>{bar}<div/>
                })
            }
        )
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks this code worked correctly, but sorry I don't have enough reputation to upvote this answer
No problem, we are upvoting :)

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.