How to spread component props inside passed node element?

Vano

My question might be confusing but let me explain with an example here, let's say I have Button component and it has 5 different props like (type, href, appearance...) and if we want to do following:

<Button type='button' href='<a { ...buttonProps }>Test Link</a>'/>

Basically attach all Button props to a tag easily.

I found something similar over here but didn't get it. Any suggestions?

Amit Chauhan

You need to change your button component and it should accept tag as property.


export default function Button({ Tag = "button", children, ...props }) {
  return <Tag {...props}>{children}</Tag>;
}

use button like this.

<Button>Test</Button>
<Button Tag="a" href="https://stackoverflow.com/" target="_blank">
  Test Link
</Button>

working codesandbox:

Edit silent-worker-0j873

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to pass props to a component passed as props in React?

Spread props for a react component

How can I access props passed in Vue router, inside a Vue component?

React - How to pass props to a component passed as prop

Enzyme, How to access a component passed in props

How to validate props that are passed to a component by value?

How to use Styled Components for a component passed as props

How do i render a react component passed in as a props with additional props?

Render component passed as props

ReactJS - Props is Not Passed to Component

How a React component is able to access props not passed to that component?

How to programmatically create a component and assign props to the passed component?

How to access id passed in Route inside Component?

How to properly access props inside a component

How to pass props to styled component inside a loop

How can I spread props to a React component that uses exact props when using Flow?

how to use props inside props in styled component in react js

How can I access props passed to React.Component

How to set state from props that is passed to the component in react?

How to properly append to string array that was passed as props in functional component

How to create a method with props passed to child component (Vue3)

How to get all props from a component that is being passed as a prop?

How to pass props to a component which is being passed as value

Test Functions Passed as Props to Component

Flow - Render component passed by props

Problems with props passed to child component

Props passed into a a component do not render

React Component not rendering a passed props

Props undefined when passed to component