所以我的代码工作正常,但我只想澄清这是否是一个好的编码实践,或者它是否会在以后引起问题。
作为一点背景,这与我之前的问题How to filter through array of components/elements in reactjs类似。但这一次,我没有过滤dom元素数组,而是过滤了一个组件数组。我是这样做的:
父:
delete_this(index)
{
let key = index._reactInternalInstance._currentElement.key;
this.repeats = this.repeats.filter( (item) =>
{
return item.key !== key;
});
this.setState({ repeats: this.repeats });
}
孩子:
delete_this(value)
{
this.props.delete_this(value);
}
render()
{
<button onClick={this.delete_this.bind(this, this)} ref={ (input) => { this.button = input; } }>delete</button>
}
我尝试对对象本身进行过滤,但它没有用,所以我改用了密钥。
答案 0 :(得分:2)
正如您在其他问题中提到的那样非常类似,您不应该依赖_reactInternalInstance
之类的内部属性。
他们&#34;私人&#34;并且React团队可以随时在技术上弃用它。我不了解Rem团队对semver的政策,但我非常怀疑内部api计数的变化是一个重大变化。
所以回答你的问题,是的,这可能会引发问题。
您可以直接将id传递给delete方法:
<button onClick={() => this.props.delete_this(this.props.id)}>delete</button>