[英]Reactjs: how to modify dynamic child component state or props from parent?
我基本上是在尝试制作标签以做出反应,但存在一些问题。
这是文件page.jsx
<RadioGroup>
<Button title="A" />
<Button title="B" />
</RadioGroup>
当您单击按钮 A 时, RadioGroup 组件需要取消选择按钮 B 。
“选定”仅表示来自状态或属性的类名
这是RadioGroup.jsx
:
module.exports = React.createClass({
onChange: function( e ) {
// How to modify children properties here???
},
render: function() {
return (<div onChange={this.onChange}>
{this.props.children}
</div>);
}
});
Button.jsx
的来源并不重要,它有一个常规的 HTML 单选按钮,可以触发原生 DOM onChange
事件
预期流量为:
这是我遇到的主要问题:我不能将<Button>
移动到RadioGroup
,因为它的结构使得孩子是任意的。 也就是说,标记可以是
<RadioGroup>
<Button title="A" />
<Button title="B" />
</RadioGroup>
或者
<RadioGroup>
<OtherThing title="A" />
<OtherThing title="B" />
</RadioGroup>
我已经尝试了几件事。
尝试:在RadioGroup
的 onChange 处理程序中:
React.Children.forEach( this.props.children, function( child ) {
// Set the selected state of each child to be if the underlying <input>
// value matches the child's value
child.setState({ selected: child.props.value === e.target.value });
});
问题:
Invalid access to component property "setState" on exports at the top
level. See react-warning-descriptors . Use a static method
instead: <exports />.type.setState(...)
尝试:在RadioGroup
的 onChange 处理程序中:
React.Children.forEach( this.props.children, function( child ) {
child.props.selected = child.props.value === e.target.value;
});
问题:什么都没有发生,即使我给Button
类一个componentWillReceiveProps
方法
尝试:我试图将父级的某些特定状态传递给子级,因此我可以更新父级状态并让子级自动响应。 在 RadioGroup 的渲染函数中:
React.Children.forEach( this.props.children, function( item ) {
this.transferPropsTo( item );
}, this);
问题:
Failed to make request: Error: Invariant Violation: exports: You can't call
transferPropsTo() on a component that you don't own, exports. This usually
means you are calling transferPropsTo() on a component passed in as props
or children.
糟糕的解决方案 #1 :使用 react-addons.js cloneWithProps方法在RadioGroup
渲染时克隆子项,以便能够传递它们的属性
糟糕的解决方案#2 :围绕 HTML/JSX 实现抽象,以便我可以动态传递属性(杀了我):
<RadioGroup items=[
{ type: Button, title: 'A' },
{ type: Button, title: 'B' }
]; />
然后在RadioGroup
动态构建这些按钮。
这个问题对我没有帮助,因为我需要在不知道他们是什么的情况下渲染我的孩子
我不确定你为什么说使用cloneWithProps
是一个糟糕的解决方案,但这里有一个使用它的工作示例。
var Hello = React.createClass({
render: function() {
return <div>Hello {this.props.name}</div>;
}
});
var App = React.createClass({
render: function() {
return (
<Group ref="buttonGroup">
<Button key={1} name="Component A"/>
<Button key={2} name="Component B"/>
<Button key={3} name="Component C"/>
</Group>
);
}
});
var Group = React.createClass({
getInitialState: function() {
return {
selectedItem: null
};
},
selectItem: function(item) {
this.setState({
selectedItem: item
});
},
render: function() {
var selectedKey = (this.state.selectedItem && this.state.selectedItem.props.key) || null;
var children = this.props.children.map(function(item, i) {
var isSelected = item.props.key === selectedKey;
return React.addons.cloneWithProps(item, {
isSelected: isSelected,
selectItem: this.selectItem,
key: item.props.key
});
}, this);
return (
<div>
<strong>Selected:</strong> {this.state.selectedItem ? this.state.selectedItem.props.name : 'None'}
<hr/>
{children}
</div>
);
}
});
var Button = React.createClass({
handleClick: function() {
this.props.selectItem(this);
},
render: function() {
var selected = this.props.isSelected;
return (
<div
onClick={this.handleClick}
className={selected ? "selected" : ""}
>
{this.props.name} ({this.props.key}) {selected ? "<---" : ""}
</div>
);
}
});
React.renderComponent(<App />, document.body);
这是一个jsFiddle,展示了它的实际效果。
编辑:这是一个更完整的动态标签内容示例: jsFiddle
按钮应该是无状态的。 而不是显式更新按钮的属性,只需更新组自己的状态并重新渲染。 Group 的 render 方法应该在渲染按钮时查看它的状态,并将“active”(或其他东西)传递给活动按钮。
也许我的是一个奇怪的解决方案,但为什么不使用观察者模式?
RadioGroup.jsx
module.exports = React.createClass({
buttonSetters: [],
regSetter: function(v){
buttonSetters.push(v);
},
handleChange: function(e) {
// ...
var name = e.target.name; //or name
this.buttonSetters.forEach(function(v){
if(v.name != name) v.setState(false);
});
},
render: function() {
return (
<div>
<Button title="A" regSetter={this.regSetter} onChange={handleChange}/>
<Button title="B" regSetter={this.regSetter} onChange={handleChange} />
</div>
);
});
按钮.jsx
module.exports = React.createClass({
onChange: function( e ) {
// How to modify children properties here???
},
componentDidMount: function() {
this.props.regSetter({name:this.props.title,setState:this.setState});
},
onChange:function() {
this.props.onChange();
},
render: function() {
return (<div onChange={this.onChange}>
<input element .../>
</div>);
}
});
也许你需要别的东西,但我发现这非常强大,
我真的更喜欢使用为各种任务提供观察者注册方法的外部模型
创建一个对象,充当父子之间的中间人。 此对象包含父级和子级中的函数引用。 然后该对象作为道具从父级传递给子级。 代码示例在这里:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.