admin管理员组文章数量:1291324
I would like to add some props to my ponent using {React.cloneElement(<MyComponent />, { onClick: () => console.log('click'), style: {background: 'red'} })}
Full code:
const MyComponent = () => {
return (
<div>
foooo
</div>
);
};
....
return React.cloneElement(<MyComponent />, { onClick: () => console.log('click'), style: {background: 'red'} })
But props
are not passed into my ponent.
When I use:
return React.cloneElement(<div>foooo</div>, { onClick: () => console.log('click'), style: {background: 'red'} })
Props are working. Why? I don`t understand why.
I would like to add some props to my ponent using {React.cloneElement(<MyComponent />, { onClick: () => console.log('click'), style: {background: 'red'} })}
Full code:
const MyComponent = () => {
return (
<div>
foooo
</div>
);
};
....
return React.cloneElement(<MyComponent />, { onClick: () => console.log('click'), style: {background: 'red'} })
But props
are not passed into my ponent.
When I use:
return React.cloneElement(<div>foooo</div>, { onClick: () => console.log('click'), style: {background: 'red'} })
Props are working. Why? I don`t understand why.
Share Improve this question asked Mar 25, 2020 at 8:13 masiyik879masiyik879 3511 gold badge4 silver badges11 bronze badges 1- cloneElement passes the props to the ponent, but your ponent must accept the props as arguments, otherwise the props are not applied. See answer by @SalientBrain – kluverp Commented Dec 14, 2022 at 13:33
2 Answers
Reset to default 4You need to apply props inside you ponent:
export const Test = (props: any) => {
return (<button {...props}>Click Me</button>);
}
In this case you can set props using
React.cloneElement(<MyComponent/>, props, null);
but I do not remend cloning (it is too heavy) I think that it is better to use special render function in props:
export const Wrapper = (props: {render: (props: any) => any}) => {
const childProps = {style: {background: 'red'}, onClick: () => { console.log('click'); }};
if (props.render){
return props.render(childProps);
} else {
return <></>;
}
}
// ...
// usage:
export default function App() {
return (
<div className="App">
<Wrapper render={(props)=>{
return <button {...props}>TEST</button>;
}}/>
</div>
);
}
Why would you use cloneElement
when you got JSX available (can conclude it from MyComponents
syntax).
Instead do:
<MyComponent
onClick={() => console.log("click")}
style={{ background: "red" }}
/>
And fix your ponent:
const MyComponent = ({ style, onClick }) => {
return <div onClick={onClick} style={style}>foooo</div>;
}
JSX is sugar syntax for createElement
/ cloneElement
.
React.cloneElement(
element,
[props],
[...children]
)
React.cloneElement() is almost equivalent to:
<element.type {...element.props} {...props}>{children}</element.type>
Therefore the right syntax:
const onClick = () => console.log('click');
const style = {background: 'red'};
// Exatcly the same effect as <MyComponent .../> above
React.cloneElement(<MyComponent/>, {onClick, style}, null);
本文标签: javascriptReact cloneElement is not working for functional componentStack Overflow
版权声明:本文标题:javascript - React cloneElement is not working for functional component - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741513685a2382756.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论