Javascript React Native-修改同一组件中的道具
我有一些简单的问题:Javascript React Native-修改同一组件中的道具,javascript,design-patterns,mobile,react-native,ecmascript-6,Javascript,Design Patterns,Mobile,React Native,Ecmascript 6,我有一些简单的问题: 是否可以修改React本机组件(非父组件)内的属性(非状态) 在[1]之后,如果我想修改同一组件中的道具,我该如何实现这一点(或者如果[1]中的答案为“否”时的变通方法) 如果我有以下资料: //母公司 render(){ return <View><ChildComponent propA={this.state.propA} /></View> } triggerChangeInParent(val) { this.setS
render(){
return <View><ChildComponent propA={this.state.propA} /></View>
}
triggerChangeInParent(val) {
this.setState({
propA: val
});
}
render(){
返回
}
触发器更改属性(val){
这是我的国家({
普罗帕:瓦尔
});
}
//子(子组件)
render(){
返回{this.props.propA}
}
triggerChangeInChild(val){
//在这里设置props.propA
}
父组件和子组件都可以修改“propA”。无论谁触发最新版本,其值将优先用于修改“propA”(例如,如果在“triggerChangeInParent”之后触发“triggerChangeInChild”,则“triggerChangeInChild”中的“val”将用于“propA”
我的问题是,我们如何实现这一目标(解决这一问题的可能解决方案/替代方案)?最佳实践/模式是什么
谢谢
contractor(props){
super(props)
this.modifyPropsOnParent = this.modifyPropsOnParent.bind(this)
}
modifyPropsOnParent(){
// do your change here
}
render() {
return <Child modifyPropsOnParent={this.modifyPropsOnParent} otherProp={yourProps} />
}
承包商(道具){
超级(道具)
this.modifyPropsOnParent=this.modifyPropsOnParent.bind(this)
}
modifyPropsOnParent(){
//找你钱吗
}
render(){
返回
}
Child可以通过此方法调用父方法。props.modifyPropsOnParent()道具是只读的。你为什么还要尝试变异道具?如果是这样的话,可能需要进行一些重构。重构可能是最终的举措。目前的更改成本相当高(由于遗留的东西,我们修复的时间非常有限)。只是向公众寻求建议,看看这是否有可能通过变通办法/解决方案实现。
contractor(props){
super(props)
this.modifyPropsOnParent = this.modifyPropsOnParent.bind(this)
}
modifyPropsOnParent(){
// do your change here
}
render() {
return <Child modifyPropsOnParent={this.modifyPropsOnParent} otherProp={yourProps} />
}