0

I currently has a component in which I have passed props into with the following render function:

render(){
    return (
        <Card>
        <CardHeader>{this.props.title}</CardHeader>
        <CardBody>
            <CardSubtitle>Count: {Object.keys(this.props.machines).length}</CardSubtitle>
            {
                Object.keys(this.props.machines).length != 0 ? 
                <ListGroup>
                    {
                        console.log('array result: ',
                            Object.keys(this.props.machines).map((machine, key) => {
                                console.log('machine name: ', machine);
                                return
                                    <ListGroupItem key={key}>
                                        {machine}
                                    </ListGroupItem>
                            })
                        )
                    }
                    {
                        Object.keys(this.props.machines).map((machine, key) => {
                            console.log('machine name: ', machine);
                            return
                                <ListGroupItem key={key}>
                                    {machine}
                                </ListGroupItem>
                        })
                    }
                </ListGroup>
                
                         : null
            }
            
        </CardBody>
    </Card>
    );
}

The problem is that the array returned by the map function contains undefined elements as seen in the console.log('array result: '...). However, the console.log('machine name: ', machine) does show individual string elements as shown in the screenshot below:

enter image description here

I have tried substituting <ListGroupItem> with other tags such as div but to no avail. Any help will be much appreciated.

2
  • 5
    automatic semicolon insertion. Beware of it. Commented Mar 19, 2018 at 5:02
  • @PatrickRoberts Wow! That is easy to overlook. Thank you for the heads up! Resolved the issue immediately Commented Mar 19, 2018 at 5:09

1 Answer 1

1

The problem is that while returning from map, your JSX is in the next line to return so while compiling, automatic semicolon is inserted after return in which case it returns undefined, you must either have the JSX element in the same line or use ()

{
     console.log('array result: ',
     Object.keys(this.props.machines).map((machine, key) => {
           console.log('machine name: ', machine);
           return (    // parenthesis here
                <ListGroupItem key={key}>
                    {machine}
                </ListGroupItem>
            )
     })
}

or

{
     console.log('array result: ',
     Object.keys(this.props.machines).map((machine, key) => {
           console.log('machine name: ', machine);
           return <ListGroupItem key={key}>
                    {machine}
                </ListGroupItem>
     })
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer :) Works now

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.