1

Trying to return multiple action buttons from a ActionButtons component:

export default class ActionButtons extends Component {
render() {
    console.log(this.props.actions)
    return(
    this.props.actions.map((field, i) => {
        <div key={field.href}>
            <DefaultButton
                key={field.href}
                text={field.label}
                href={field.href}
            />
        </div>
    })
    )
    }
}

Calling it from another component with the following code:

      const actions = [
            {"label": "Go Back", "href":"www.google.com"}
        ];
<ActionButtons actions={actions} />

On the ActionButtons component if i return just one single button without the map then it works. What am i missing ?

0

1 Answer 1

2

You need to explicitly return your jsx from inside map

//inside render
return this.props.actions.map((field, i) => {
    return (
        <div key={field.href}>
            <DefaultButton
                key={field.href}
                text={field.label}
                href={field.href}
            />
        </div>
    )
}

When using a jsx block () (the example above returns an array which is also valid) you also need to declare your operations inside curly brackets.

export default class ActionButtons extends Component {
    render() {
        console.log(this.props.actions)
        return (
            <>
            {
                this.props.actions.map((field, i) => {
                    return (
                        <div key={field.href}>
                            <DefaultButton
                                key={field.href}
                                text={field.label}
                                href={field.href}
                            />
                        </div>
                    )
                })
            }
            </>
        )
    }
}
Sign up to request clarification or add additional context in comments.

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.