gpt4 book ai didi

javascript - Reactjs - 将相同的 Prop 传递给多个子组件

转载 作者:行者123 更新时间:2023-11-28 13:01:20 25 4
gpt4 key购买 nike

我有一个父组件,它有多个子组件。为子组件传递的 props 都是相同的。例如

<TextQuestion questionInfo={this.props.questionInfo} key={this.props.questionInfo.module_question_id} handleChange={this.handleChange} />

<DropDown questionInfo={this.props.questionInfo} key={this.props.questionInfo.module_question_id} handleChange={this.handleChange} />

<Checkbox questionInfo={this.props.questionInfo} key={this.props.questionInfo.module_question_id} handleChange={this.handleChange} />

有没有更有效的方法来做到这一点?可能是返回 Prop 并添加到子组件的函数调用?

最佳答案

const someProps = {
questionInfo: this.props.questionInfo,
key: this.props.questionInfo.module_question_id,
handleChange: this.handleChange,
}

<TextQuestion {...someProps} />
<DropDown {...someProps} />

或者,如果组件是同级组件,您可以将它们放入数组中并执行 map:

{[TextQuestion, DropDown, Checkbox].map((Component, i) => <Component key={i} questionInfo={...} />}

关于javascript - Reactjs - 将相同的 Prop 传递给多个子组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50171934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com