我试图从一个阵营无国籍子组件(AddConditionSelect
)到母部件(AddConditionDashboard
)通过输入值字段(conditionTitle
),将保持我的状态。
问题
我跟着React documentation所示的模型,但他们使用的是裁判,这只有在组件是有状态的作品。我不想在子组件中设置任何状态,但仍然能够访问父项中的输入。
在目前的形式中,我得到一个警告,无状态函数组件不能被给予引用,导致道具为空和未定义。
父组件:
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;
您能不能告诉我们您的输入组件?这将有所帮助。 – QoP
输入组件只是一个Rebass组件 - http://jxnblk.com/rebass/#Input – Dan