【发布时间】:2017-02-19 04:29:49
【问题描述】:
我有一个关于道具和功能组件的看似微不足道的问题。基本上,我有一个容器组件,它在用户单击按钮触发的状态更改时呈现模态组件。 modal 是一个无状态的功能组件,它包含一些需要连接到容器组件内的功能的输入字段。
我的问题:当用户与无状态模态组件内的表单字段交互时,如何使用父组件内的函数来更改状态?我是否错误地传递了道具?提前致谢。
容器
export default class LookupForm extends Component {
constructor(props) {
super(props);
this.state = {
showModal: false
};
}
render() {
let close = () => this.setState({ showModal: false });
return (
... // other JSX syntax
<CreateProfile fields={this.props} show={this.state.showModal} onHide={close} />
);
}
firstNameChange(e) {
Actions.firstNameChange(e.target.value);
}
};
功能(模态)组件
const CreateProfile = ({ fields }) => {
console.log(fields);
return (
... // other JSX syntax
<Modal.Body>
<Panel>
<div className="entry-form">
<FormGroup>
<ControlLabel>First Name</ControlLabel>
<FormControl type="text"
onChange={fields.firstNameChange} placeholder="Jane"
/>
</FormGroup>
);
};
示例:假设我想从 Modal 组件中调用 this.firstNameChange。我猜想将道具传递给功能组件的“解构”语法让我有点困惑。即:
const SomeComponent = ({ someProps }) = > { // ... };
【问题讨论】:
标签: javascript reactjs components