- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 {React.cloneElement(<MyComponent />, { onClick: () => console.log('click'), style: {background: 'red'} })}
向我的组件添加一些 Prop
完整代码:
const MyComponent = () => {
return (
<div>
foooo
</div>
);
};
....
return React.cloneElement(<MyComponent />, { onClick: () => console.log('click'), style: {background: 'red'} })
但是props
没有传递到我的组件中。
当我使用时:
return React.cloneElement(<div>foooo</div>, { onClick: () => console.log('click'), style: {background: 'red'} })
Prop 正在发挥作用。为什么?我不明白为什么。
最佳答案
当 JSX 可用时,为什么要使用 cloneElement
(可以从 MyComponents
语法中得出结论)。
改为:
<MyComponent
onClick={() => console.log("click")}
style={{ background: "red" }}
/>
并修复你的组件:
const MyComponent = ({ style, onClick }) => {
return <div onClick={onClick} style={style}>foooo</div>;
}
JSX is sugar syntax对于 createElement
/cloneElement
。
React.cloneElement(
element,
[props],
[...children]
)
React.cloneElement() 几乎等同于:
<element.type {...element.props} {...props}>{children}</element.type>
因此正确的语法:
const onClick = () => console.log('click');
const style = {background: 'red'};
// Exatcly the same effect as <MyComponent .../> above
React.cloneElement(<MyComponent/>, {onClick, style}, null);
关于javascript - React cloneElement 不适用于功能组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60844971/
我正在尝试使用 React 和 Typescript 制作仪表板。我使用 Card 组件通过 React.cloneElement 将子组件推送到其中,并添加 Card 的 setStates 以从子
我想使用 {React.cloneElement(, { onClick: () => console.log('click'), style: {background: 'red'} })} 向我的
使用 React.cloneElement 会导致我似乎无法解决的类型错误。 class Dropdown extends React.Component> }> { render() {
如果我有的话,originalComponent 是否会被垃圾回收: const WrapperComponent = ({ originalComponent, ...props }) => {
我很难理解 React.cloneElement() 函数的行为 我的组件结构是这样的 A.js export default class A extends React.Component {
我正在尝试创建一个高阶组件 Hoc,它通过 React.cloneElement 为其子组件提供一些额外的 Prop 。我一直无法让 flowtype 知道额外的 Prop 实际上已经被传递了下来。
我想解决此库的问题:react-form 。有关信息,这是我当前的错误: Uncaught Error: Element type is invalid: expected a string (for
我正在使用 React 和 Typescript。我有一个充当包装器的 react 组件,我希望将它的属性复制到它的子组件。我正在遵循 React 的克隆元素使用指南:https://facebook
我正在构建一个动态生成键的表控件(我理解这可能不是一个好主意——我认为键应该与它所代表的数据唯一关联,否则 React 只能为我们生成唯一的 ID?),但无论哪种方式,似乎都没有设置 key ,我不知
我有同样的问题,我已经使用了 create-react-app我的 react-toastify 版本是 ^9.0.8。请帮我这是完整的消息:./node_modules/react-toastify
如何使用 React.cloneElement 附加或更改 className 属性? 当我使用 React.cloneElement 时,我无法更改或附加 className Prop 。我已经搜索
我有 TestWrapper克隆 element 的组件并且应该使背景变为蓝色。 Test也在做同样的事情,但设置 color .我期待渲染的元素有蓝色背景和红色文本,但它只是 red .这是示例:
我正在使用 React 组合一个表格控件,它几乎一直遵循这种模式一直到单元格: class Table extends Component { static propTypes = {
我不明白 React official docs 中所写陈述的意义: cloneElement() React.cloneElement( element, [props], [...ch
我一直在测试使用 React.cloneElement() 扩展组件的 children 可能存在的限制/危险。我发现的一种可能的危险是可能会覆盖 ref 和 key 等 Prop 。 但是,根据 R
任何人都可以告诉我使用 cloneElement(在现有元素实例上)还是 createElement(在 React Element 类上)哪个在性能方面更好? 有时克隆某些东西比创建新实例更快。请告
我想知道当您将 ES6 和 cloneElement 传递给函数时它是如何工作的。我需要在父组件的状态中引用状态,但 this 引用子组件而不是父组件。 下面是使它工作的常规 JavaScript 代
我正在学习 React 并试图找出动态添加 props 的最佳方法。我知道两种方法可以做到这一点,但不知道哪一种更好更快。 第一种方法是使用 React.cloneElement api const
假设我有一个具有 className 的 ReactElement,并且我想向其 className 添加一个新类,而不覆盖现有的 className。我该怎么做? 我尝试了以下方法,但它确实覆盖了现
React.cloneElement() 总是需要第一个参数作为 react 组件,它应该作为 Prop 中的 child 传递。 有没有办法将一个简单的 HTML 节点作为子节点传递。请引用下面的代
我是一名优秀的程序员,十分优秀!