[英]React Hooks - How can I refactor these dynamically generated inputs?
I am trying to use dynamically created inputs, and was wondering how I could make the code better.我正在尝试使用动态创建的输入,并且想知道如何使代码更好。
The idea is to have a "configuration" object which determines the input generation.这个想法是有一个“配置” object 来确定输入生成。
I am interested in all suggestions, like better patterns, renaming variables/components/css-classes, moving code around, possible problems, inefficiencies...我对所有建议都很感兴趣,比如更好的模式、重命名变量/组件/css-classes、移动代码、可能的问题、效率低下......
My possible improvement is to move the input state inside the Inputs
component, rather than the current Journal
component.我可能的改进是将输入 state 移动到
Inputs
组件内,而不是当前的Journal
组件内。 Not sure if it's a good idea...不确定这是不是个好主意...
Journal.js日记.js
import React, { useState, useEffect } from "react";
import Inputs from '../layout/Inputs';
function Journal(props) {
const [inputsJournalItems, setInputsJournalItems] = useState({
documentKey: "",
documentDate: ""
});
// generic handler based on the input name
const handleInputChange = e => {
setInputsJournalItems({
...inputsJournalItems,
[e.target.name]: e.target.value
})
}
// log the state after re-render
useEffect(() => {
console.log(inputsJournalItems)
}, [inputsJournalItems]);
// how to avoid including the handler for each input?
const inputs = [
{ type: "input", label: "Document", name: "documentKey", handleInputChange },
{ type: "input", label: "Date", name: "documentDate", handleInputChange },
{ type: "button", value: "Save", name: "save" },
];
return (
<div>
<Inputs
data={inputsJournalItems}
inputs={inputs} />
</div >
)
}
export default Journal;
Inputs.js Inputs.js
import React from 'react';
import Input from '../layout/Input';
function Inputs(props) {
return (
<div className="inputs">
{
props.inputs.map(input => {
return (
<Input
type={input.type}
label={input.label}
name={input.name}
handleInputChange={input.handleInputChange}
value={input.value ? input.value : props.data[input.name]}
>
</Input>
)
})
}
</div>
)
}
export default Inputs;
Input.js输入.js
import React from 'react';
function Input(props) {
let inputId = `input-${props.name}`;
return (
<div className="input-group">
<label>{props.label}</label><br></br>
<input
type={props.type}
id={inputId}
name={props.name}
onChange={props.handleInputChange}
value={props.value}
/>
</div>
)
}
export default Input;
I have a few suggestions and a few modifications that may make things cleaner and maintainable for you:我有一些建议和一些修改可能会让您的事情变得更清洁和可维护:
you can check it out on codesandbox.io你可以在codesandbox.io上查看
or by running the snippet below或者通过运行下面的代码片段
<iframe src="https://codesandbox.io/embed/nervous-snow-sefyi?fontsize=14" style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;" title="nervous-snow-sefyi" allow="geolocation; microphone; camera; midi; vr; accelerometer; gyroscope; payment; ambient-light-sensor; encrypted-media; usb" sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin" ></iframe>
Demo Screenshot
演示截图
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.