reactjs - React:在子组件中改变 Ref 对象的实例
问题描述
class Parent extends React.Component {
constructor(props) {
super(props)
this.data = React.createRef()
}
componentDidMount() {
this.data.current = []
}
render() {
return <Child data={data} />
}
}
const Child = ({ data }) => {
const onDelete = (idx) => {
data.splice(idx, 1)
}
return (
<button onClick={(index) => onDelete(index)}>Delete</button>
)
}
我需要这个 ref 是一个数组来解释一些复杂的原因,尽管代码正在完成这项工作,但我不确定ref
在子组件中改变 prop 是否正确。我的问题是:如果我传递给子组件的道具不是父组件中的状态,那么可以对其进行变异吗?或者在裁判的情况下,它的工作方式不同吗?提前感谢您的帮助!
解决方案
推荐阅读
- facebook-opengraph - Facebook OpenGraph 看不到我的元标记
- angular - 如何在cordova应用程序上实现没有googleplus插件的Google OAuth登录?
- r - 返回相应的行名而不是 r 中的数据
- django - django中同类型关系的多种类型
- visual-studio-2010 - CMake 为 VS2017 生成的项目需要安装 VS2010
- namespaces - 可以描述 SPARQL 查询的 RDF 命名空间?
- flutter - 布尔参数作为 null 传递给小部件
- database - Spring boot JPA - 使用额外的列查询多对多
- python - 有什么方法可以添加到 python 中的 .replace 函数?
- c# - 具有用户定义值集合的 ML.NET 价格预测模型