[英]Display multiple dynamic input fields in react, based on a changing state variable?
So, I am building my first React app, that connects to a Flask backend.所以,我正在构建我的第一个 React 应用程序,它连接到 Flask 后端。 I have a specific functional component that fetches a dictionary from the Python program, and I have saved this in a state variable(done using useState, setVariables).我有一个特定的功能组件,可以从 Python 程序中获取字典,并且我已将其保存在 state 变量中(使用 useState、setVariables 完成)。 The object is of type object 是类型
{'A':'x', 'B':'y', 'C':2},
and these are variable , and each render needs to be dynamic, based only on this object.这些是可变的,并且每个渲染都需要是动态的,仅基于此 object。 Based on the current state of this object, I want to display key-value pairs of this object in基于这个object的当前state,我想在这个object中显示键值对
<label>insertkey</label>
<input type="text" name = "insertkey" value=insertvalue/>
In addition, I want to be able to save any changes made to these values, and write into the state variable, so that I can send them back to my Flask backend.此外,我希望能够保存对这些值所做的任何更改,并将其写入 state 变量,以便我可以将它们发送回我的 Flask 后端。
I tried looping through each, and inserting as follows:我尝试遍历每个,并插入如下:
const [fields, setFields] = useState({});
//intermediate code for fetching and setting the state
useEffect(() => {
for (const [key, value] of Object.entries(fields)) {
console.log(`${key}:${value}`);
insert = insert + `<label> ${key} </label> <input type = 'text' value = ${value} name = ${key} onChange=${changedData()}></input><br/>`
}
document.getElementById('fields_form').innerHTML = insert;
});
const changedData = (evt) => {
setFields({ [evt.target.name]: evt.target.value });
console.log(evt.target.value);
}
//some intermediate code
return (
<div>
<form onSubmit={submitFunction}>
<p id='fields_form'>
</p>
<input type="submit" value="Submit"></input>
</form>
</div>
);
But when I do this, the state is set as 'Undefined'.但是当我这样做时,state 被设置为“未定义”。
I would love to hear about some ways to implement this.我很想听听一些实现这一点的方法。 Maybe it's a really small fix, and I'm missing it, or perhaps there is a different approach that could be more suitable.也许这是一个非常小的修复,我错过了它,或者也许有一种不同的方法可能更合适。
Simple:简单的:
const Form = () => {
const [values, setValues] = useState({});
const onChange = (e) => {
const name = e.target.name;
let value = e.target.value;
setValues({ ...values, [name]: value });
};
const onSubmit = (e) => {
// send data or save it to local store
api.send(values)
};
return (
<form encType="multipart/form-data">
<input
type="text"
name="email"
value={value["email"]}
onChange={onChange}
/>
<input
type="text"
name="subject"
value={value["subject"]}
onChange={onChange}
/>
<input
type="text"
name="body"
value={value["body"]}
onChange={onChange}
/>
>
<button type="submit" onClick={onSubmit}>
Save
</button>
</form>
);
};
export default Form;
console.log(values);
It's your solution.这是你的解决方案。
const [fields, setFields] = useState({});
const [values, setValues] = useState({});
// some fetch logic
// happens on loading some data
// for example data structure look's like:
// fetchedData = {
// email: 'email@placeholder'
// subject: 'subject placeholder'
// body: 'body placeholder'
// }
useEffect(() => {
if (fetchedData) {
setFields(fetchedData)
}
}, [fetchedData]);
// happens on fields changed
useEffect(() => {
if (fields) {
setValues(fields)
}
}, [fields]);
const changedData = (evt) => {
setValues({ ...values, [evt.target.name]: evt.target.value });
}
return (
<div>
<form onSubmit={submitFunction}>
{Object.keys(fields).map(_ => (
<input
type="text"
name={_}
value={values[_]}
onChange={changedData}
/>
))}
<input type="submit" value="Submit"></input>
</form>
</div>
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.