javascript – 通过道具将值从React Stateless Child传递给Parent
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 通过道具将值从React Stateless Child传递给Parent,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2899字,纯文字阅读大概需要5分钟。
内容图文
CONTEXT
我正在尝试将输入值字段(conditionTitle)从React无状态子组件(AddConditionSelect)传递到将保存我的状态的父组件(AddConditionDashboard).
问题
我遵循了React documentation中显示的模型,但它们使用的是refs,只有在组件有状态时才有效.我不想在子组件中设置任何状态,但仍然可以访问父组件中的输入.
在其当前形式中,我得到一个警告,无状态函数组件不能被赋予refs,导致props为null和undefined.
父组件:
import AddConditionSelect from '../containers/AddConditionSelect.js';
class AddConditionDashboard extends React.Component {
constructor(props) {
super(props);
this.state = {
conditionTitle: '',
conditionType: ''
};
}
handleUserInput({conditionTitleInput}) {
this.setState({
conditionTitle:conditionTitle
})
}
render() {
const {error, segmentId} = this.props;
return (
<div>
<AddConditionSelect segmentId={segmentId} conditionTitle={this.state.conditionTitle} onUserInput={this.handleUserInput} />
<PanelFooter theme="default">
<Button backgroundColor="primary" color="white" inverted={true} rounded={true} onClick={(event) => this.onSubmit(event)}>
Next Step
</Button>
</PanelFooter>
</div>
);
}
}
export default AddConditionDashboard;
子组件:
class AddConditionSelect extends React.Component {
onInputChange: function() {
this.props.onUserInput(
this.refs.conditionTitleInput.value,
)
},
render() {
const {error} = this.props;
return (
<div>
<Panel theme="info">
<Divider />
Please enter a name {error ? <Message inverted={true} rounded={true} theme="error">{error}</Message> : null}
<Input value={this.props.conditionTitle} ref="conditionTitleInput" label="" type="text" buttonLabel="Add Condition" name="add_segment" onChange={this.onInputChange} placeholder="Condition Title"/>
</Panel>
</div>
);
}
}
export default AddConditionSelect;
最佳答案:
如何将事件处理程序直接传递给< Input>?这样您就可以将on change事件直接传递给您的父级(祖父母< Input>),并且您可以从event.target.value中提取值,因此无需使用refs:
注意:您可能必须在父级构造函数中绑定onUserInputChange()的上下文,因为事件处理程序默认情况下将事件发生的元素作为其上下文:
亲
class AddConditionDashboard extends React.Component {
constructor(props) {
// ...
// bind the context for the user input event handler
// so we can use `this` to reference `AddConditionDashboard`
this.onUserInputChange = this.onUserInputChange.bind(this);
}
onUserInputChange({ target }) {
const { value: conditionTitle } = target;
this.setState({
conditionTitle
});
}
render() {
// ...
<AddConditionSelect segmentId={segmentId}
conditionTitle={this.state.conditionTitle}
onUserInputChange={this.onUserInputChange} // <-- pass event handler to child that will pass it on to <Input>
/>
// ...
}
// ...
儿童:
class AddConditionSelect extends React.Component {
render() {
const { error } = this.props;
return (
<div>
// ...
<Input value={this.props.conditionTitle}
label=""
type="text"
buttonLabel="Add Condition"
name="add_segment"
onChange={this.props.onUserInputChange} // <-- Use the grandparent event handler
placeholder="Condition Title"
/>
// ...
</div>
);
}
}
内容总结
以上是互联网集市为您收集整理的javascript – 通过道具将值从React Stateless Child传递给Parent全部内容,希望文章能够帮你解决javascript – 通过道具将值从React Stateless Child传递给Parent所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。