gpt4 book ai didi

javascript - 通过 reac for 中的 e.target 从映射表单中获取值

转载 作者:行者123 更新时间:2023-12-02 21:07:39 25 4
gpt4 key购买 nike

I am displaying multiple forms by mapping in react, and submit button is in parent component, how to getvalues of all the forms which are mapped on form submit?

below is my code for parent component:

<form>
{rowdata.map((item, index) => <ChildForm key={index} {...this.state} />)}
<Button type="submit" className="primarybtn btnsmall"/>
</form>

最佳答案

将整个表单放入组件中,然后简单地将组件与其中的表单和按钮进行映射。

只需传递 Prop 即可。

父组件。

return (
{someArray.map(form => (
<SomeForm onSubmit={this.formSubmitHandler} key={form.key} />

)}

子组件。

import React from 'react;

function SomeForm(props) {
return (
<div>
<form onSubmit={props.onSubmitHandler}>
Your Form Here
<Button type="submit" className="primarybtn btnsmall"/>
</form>
)}

关于javascript - 通过 reac for 中的 e.target 从映射表单中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61184477/

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