hh54188 hh54188 - 8 months ago 124
React JSX Question

React js: How to set props in JSX components which store in variable

Suppose I already define an component:

class Co extends React.Component {
render = () => {
const name = this.props.name;
return (
<p>Hello, my name is {name}</p>

and store it in an variable:

const co = <Co />;

How can I set the component's props with the variable? Would


As I understand you don't want to render your component in JSX syntax but with your stored variable. You can have a look at React.cloneElement. This should do what you want:

{React.cloneElement(co, {name: 'hans'})}

See: https://facebook.github.io/react/docs/top-level-api.html#react.cloneelement