我希望基于 json 生成输入,所以首先我将它设置为初始状态,然后在子组件中我想修改它的字段,事情是组件没有更新......它渲染一次并且不知道如何使它成为每次输入 onChange 更改其值时都会更新。知道每次输入内容时如何更新输入值吗?
家长
function App() {
const [inputValue, setInputValue] = useState(chunkingRow);
const handleChunkingChange = (e, index) => {
let inputContent = inputValue;
const reg = new RegExp(/^[0-9]+$/);
if (e.target.value.match(reg)) {
inputContent[index] = {
...inputContent[index],
content: e.target.value,
};
setInputValue(inputContent);
console.log(inputValue);
} else console.log('not a number')
};
return (
<div>
<Wrapper>
{Chunk(inputValue, handleChunkingChange)}
</Wrapper>
</div>
);
}
孩子
const Chunk = (inputValue, handleChunkingChange) => {
return(
<div>
{inputValue.map((el, index) => (
<div key={index}>
<p>{el.title}</p>
{console.log(el.content)}
<input
type="text"
onChange={(e, i) => handleChunkingChange(e, index)}
value={el.content}
/>
</div>
))}
</div>
);
}
链接到演示 https://codesandbox.io/s/stoic-mirzakhani-46exz?file=/src/App.js