【发布时间】:2021-04-04 17:27:27
【问题描述】:
我对 React/Next 还很陌生,我有一个简短的问题。
我正在尝试创建一个按钮来实时增加 div 的数量。
这是我的代码:
import React from 'react'
const Clown = () => {
const [clownCounter, setClownCounter] = React.useState(1);
function addClown(event) {
event.preventDefault();
}
return(
<React.Fragment>
<div>
<form>
{Array.from({ length: clownCounter}, (_unused, index) => index + 1).map(
(clownIndex) => {
const clownid = `${clownIndex}`
return (
<div key={clownid } className="clown-box">
<label htmlFor={clownid }>Activity {clownIndex}</label>
<br />
<input type="text" onChange={(e)=> onChangeForm(e)} name={activityId} id={activityId} />
<br />
</div>
)
},
)}
<span className="clown-add">
<button onClick={addClown} onChange={() => { setClownCounter(clownCounter++) }}>Add Clown</button>
</span>
<br />
</form>
</div>
</React.Fragment>
)
}
export default Clown
如您所见,目标是在每次单击按钮时增加小丑框 div 的数量。我想我很接近,但它目前不起作用。有人可以帮忙吗?
【问题讨论】:
-
Array.from({ length: clownCounter}没有按照您的预期进行。
标签: javascript reactjs for-loop web frontend