如何在 React.js 中向组件实例添加 props? [英] How to add props to a component instance in React.js?
本文介绍了如何在 React.js 中向组件实例添加 props?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否可以向组件实例添加新的 prop,组件实例如下所示:
I would like to know if it is posible to add a new prop to a component instance, the component instance look like this:
直接修改组件实例可能是一种不好的做法,但我想知道是否可能.
Maybe is a bad practice modifying a component instance directly, but I wonder if it is possible or not.
推荐答案
您可以使用 React.cloneElement
来做到这一点:
You can do this by using React.cloneElement
:
const changeProp = (component) => (
{React.cloneElement(component, {
someProp: { value }
})}
)
React 文档参考:https://reactjs.org/docs/react-api.html#cloneelement
Reference to react docs: https://reactjs.org/docs/react-api.html#cloneelement
这篇关于如何在 React.js 中向组件实例添加 props?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文