How to add additional props to a React element passed in as a prop?

sme

I am passing a react element as a prop to another element. In the child element that receives the prop, I need to set additional props to that element.

For example:

Parent class

class Menu Extends React.Component {
    render() {
        return(
            <div className="Menu">
                <MenuItem icon={<MdInbox />} />
                <MenuItem icon={<MdDrafts />} />
                <MenuItem icon={<MdTrash />} />
            </div>
        );
    }
}

Child class

class MenuItem Extends React.Component {
    render() {
        return(
            <div className="MenuItem">
                {this.props.icon} // I want to set the icon's size prop here
            </div>
        );
    }
}

this.props.icon is a React element (<MdInbox />, <MdTrash />, etc), and it allows for a property size. I want to set the size property in the MenuItem class, as opposed to passing the prop in from the parent like this: <MenuItem icon={<MdInbox size={24} />}. I'd prefer just to set the size in one place only, within the MenuItem class.

Denis

Pass in the component constructor instead of an instance:

class Menu extends React.Component {
    render() {
        return(
            <div className="Menu">
                <MenuItem icon={MdInbox} />
                <MenuItem icon={MdDrafts} />
                <MenuItem icon={MdTrash} />
            </div>
        );
    }
}

The child class:

class MenuItem extends React.Component {
    render() {
        // This constant must begin with a capital,
        // it’s how React distinguishes HTML elements from components.
        const Icon = this.props.icon;
        return(
            <div className="MenuItem">
                <Icon size={24} />
            </div>
        );
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

In React how to add key prop to element passed as Object

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

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

Adding additional props to a component passed as a prop

How to add additional component as prop to a reusable component in React?

How to check what additional props were passed into a react component that were not defined?

How to add a prop to a specific element by cloning the element in React?

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

React - passing component prop in click handlers passed as props

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

How to add an onClick event on a React Component that is passed down to another component as a prop

How to add two functions passed as props to an onChange event in React and Material UI?

How to add an anchor in React props?

How to pass in props to an element that changes in size depending on prop

How to add an additional element to multiple json files

How to add additional properties to be passed through sequelize object in express

How to spread component props inside passed node element?

How do you pass additional props to a functional component in React?

In React, how do I render a stateless component that is passed in as a prop?

How to evaluate the passed prop to the styled component using react and typescript?

How to change the value of a prop of Sub Component passed as a column to React Table?

How to type async function passed as prop to React component

How to set a type for a React Compnent being passed in as a prop

How to NOT render/ hide a React component when no prop is passed?

How to correctly infer React props from as prop with generic overload

React. How to pass props inside a component defined on a prop?

Pass in Additional Props to Div - React

How to add attribute to HTML tag passed in props- before render?

Call a method passed as a prop in react