Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing props in a component that is stored in a variable

So I have a simple and straight up question.

So we can store react components in a variable. But now, I want to know if it is possible pass props to a component if it is stored in a variable?

//////////////////////////////////////////////
//////  Normal & typical React component
//////////////////////////////////////////////

const myComponent = <Hello name={'Jamie'} /> 

render () {

  return myComponent;

}


////////////////////////////////////////
//////  What I want
////////////////////////////////////////

const myComponent = <HelloWorld /> 

// I want to be able to add that props here
// something like myComponent.addProps({'name': 'jamie'});

render () {

  return myComponent;

}

My problem is well beyond just this. I wanted to know if we have such a feature in React API.

like image 819
Jamie Jamier Avatar asked Jan 20 '19 04:01

Jamie Jamier


People also ask

How do you pass a variable as a prop in React?

Just call an alert method in the childToParent function and pass that function as a prop to the child component. And in the child component, accept the childToParent function as a prop. Then assign it to an onClick event on a button. That's it!

How do I pass Props to components?

There is no way to pass props up to a parent component from a child component. We will revisit this caveat later in this tutorial. It's also important to note that React's props are read only (immutable). As a developer, you should never mutate props but only read them in your components.

Can we pass a component in props?

You can pass a component as props in React by using the built-in children prop. All elements you pass between the opening and closing tags of a component get assigned to the children prop.

Can you pass props to a class component?

Props in React get passed from the parent to the child. For instance, If you have a parent class which renders a child class, the parent class can now pass props to the child class.


2 Answers

You can try like following,

React.cloneElement(
  myComponent,
  {prop1: "prop value"}
)

Reference Doc: React Official Doc

like image 88
Mohammad Ashraful Islam Avatar answered Oct 17 '22 20:10

Mohammad Ashraful Islam


Went through kind of same issue in typeScript, had a list of menuItem to map on it in JSX, each one containing a different React Component for icon like this:

const menuItems: {label: string, icon: React.FC, selected:boolean}[] = [
{label: 'Home', icon: <HomeIcon/>, selected: false}, 
{label: 'Live TV', icon: <ScreenIcon/>, selected: false}, 
{label: 'VOD', icon: <PlayIcon/>, selected: true},
{label: 'Podcasts', icon: <MicIcon/>, selected: false},
{label: 'Games', icon: <GameIcon/>, selected: false},
{label: 'App', icon: <MenuIcon/>, selected: false},
{label: 'Services', icon: <ShopIcon/>, selected: false},
{label: 'Languages', icon: <WorldIcon/>, selected: false},];

But I needed to give a "fill" props to the icon if the menuItem was selected or focused, so I changed my array like this:

const menuItems: {label: string, icon: React.FC, selected:boolean}[] = [
{label: 'Home', icon: HomeIcon, selected: false}, 
{label: 'Live TV', icon: ScreenIcon, selected: false}, 
{label: 'VOD', icon: PlayIcon, selected: true},
{label: 'Podcasts', icon: MicIcon, selected: false},
{label: 'Games', icon: GameIcon, selected: false},
{label: 'App', icon: MenuIcon, selected: false},
{label: 'Services', icon: ShopIcon, selected: false},
{label: 'Languages', icon: WorldIcon, selected: false},];

And Called it like this:

    const FocusableListItem: FC<ExportableProps> = (props) => {
  const classes = useStyles(props);

  return (
    <ListItem button key={props.label} className={classes.button} >
      <ListItemIcon>
        <props.icon fill={props.focused || props.selected ? '#6648ff' : '#c4c4c4'}></props.icon>
      </ListItemIcon>
      <ListItemText>{props.label}</ListItemText>
    </ListItem>
  );
};

With this kind of interface for props:

interface ExportableProps extends withFocusableProps {
  label: string;
  icon: React.FC<{fill: string}>;
  onClick?: () => void;
  disabled?: boolean;
  selected?: boolean;
  small?: boolean;
}

And works perfectly ! Don't know if it'll help someone but looks useful.

like image 1
Pickymtr Avatar answered Oct 17 '22 21:10

Pickymtr