0

I have a function type react component:

function Card(props, {icon}) {
    return (
        <div className="card">
            <FontAwesomeIcon icon={icon} className="icon"/>
            <h3 className="text">{props.name}</h3>            
        </div>
    )
}

and I want to have 2 attributes for it: name and icon But to display the name in my title I need to use the props.name but the icon is just an attribute. When I pass the arguments like that I only get the name, and if I only pass the icon, I'm able to get the icon, but I can't manage to have them both. (The icon is a Fontawesome component).

I'm calling it in my App.js like this:

<Card name="Folder" icon="folder"/>

How can I make this work?

1 Answer 1

1
<Card name="Folder" icon="folder"/>

Your props object now looks like

const props = {
   name: "Folder",
   icon: "folder",
}

we can then use object destructuring when we pass it into our component.

function Card({icon, name}) {
    return (
        <div className="card">
            <FontAwesomeIcon icon={icon} className="icon"/>
            <h3 className="text">{name}</h3>            
        </div>
    )
}
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent, be sure to make it as the answer.

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.