【发布时间】:2021-12-24 03:48:20
【问题描述】:
我是 React 新手,正在尝试将数据(director、nonDirector)从我的子功能组件传递到父类组件。
我的代码如下,我需要做什么才能让它工作?
类父组件
export default class ProfileEditor extends React.Component<IProfileEditorProps, IProfileEditorState> {
constructor(props: IProfileEditorProps) {
super(props);
this.state = {
data: null,
}
this.updateProfile = this.updateProfile.bind(this);
}
public updateProfile(director, nonDirector) {
console.log('hello', director, nonDirector);
}
<MultiSelect
options={ profile?.Declarations.length == 0 ? countries : this.combineProfileAndCountrys() }
updateProfile={updateProfile()}
/>
子功能组件
export const MultiSelect = ({ options, updateProfile }) => {
const [nonDirector, setNonDirector] = React.useState();
const [director, setDirector] = React.useState();
<PrimaryButton text='Save' onClick={ updateProfile(nonDirector, director) } />
</Fragment>
);
【问题讨论】:
标签: javascript reactjs