【发布时间】:2021-11-21 16:16:58
【问题描述】:
我有一个Parent 组件,它接收child 和onSubmit 道具。 child 是具有任意 props 的任意功能组件。
例如
const Child = ( { name, onNameChange } ) => <> ... </>
// or
const Child = ( { description, onDescriptionChange } ) => <> ... </>
我想把子组件的props存储在父组件里面,以便
- 孩子对自己的道具进行更改(通过使用自己的道具)
- 当单击提交按钮时,父级可以使用子级道具将它们传递给提交函数。
const Parent = (
{
child: Child,
onSubmit,
},
) => {
const childProps = 'What do I put here ?';
const handleSubmit = () => {
onSubmit(childProps);
};
return (
<>
<Child { ...childProps } />
<button onClick={ () => { return handleSubmit(); } }>Submit</button>
</>
);
};
我该怎么做?
【问题讨论】:
-
您是否考虑过使用上下文? reactjs.org/docs/context.html
-
上下文对我的情况有何帮助?解决方案应该仍然与
childProps无关,这是我正在努力寻找的
标签: reactjs react-props react-component react-state