如何在React中使用map创建独立的子元素
我可以成功地使用 map 从数组内部创建多个元素,但我正在创建的按钮以及分配给每个子元素的状态似乎与第一个映射相关联。此处的代码和屏幕截图。
import React, {useState} from 'react';
import './Switch.css'
const Switch = () => {
const [status, setStatus] = useState(true);
const meats = [
"Chicken",
"Ground Beef",
"Sausage"
]
const meatSwitches = meats.map((meat) =>
<>
<div id="ingredientContainer" className={status === true ? 'containerYes' : 'containerNo'}>
<h2 id='ingredientLabel'>{meat}</h2>
<div id="underLabel">
<h3 id='yes'>Sounds Yummy! </h3>
<input
className="react-switch-checkbox"
id={`react-switch-new`}
type="checkbox"
onClick={() => setStatus(!status)}
/>
<label
className="react-switch-label"
htmlFor={`react-switch-new`}
>
<span className={`react-switch-button`} />
</label>
<h3 id='no'>No, thanks.</h3>
</div>
</div>
</>
);
如何让每个子元素都有一个功能按钮,独立于第一个元素并具有独立的状态(在本例中为“true”和“false”)。
I can successfully use map to create multiple elements from inside my array, but the button that I'm creating and the state I'm assigning to each child element seems to be tied back to the first mapping. Code here along with screen shot.
import React, {useState} from 'react';
import './Switch.css'
const Switch = () => {
const [status, setStatus] = useState(true);
const meats = [
"Chicken",
"Ground Beef",
"Sausage"
]
const meatSwitches = meats.map((meat) =>
<>
<div id="ingredientContainer" className={status === true ? 'containerYes' : 'containerNo'}>
<h2 id='ingredientLabel'>{meat}</h2>
<div id="underLabel">
<h3 id='yes'>Sounds Yummy! </h3>
<input
className="react-switch-checkbox"
id={`react-switch-new`}
type="checkbox"
onClick={() => setStatus(!status)}
/>
<label
className="react-switch-label"
htmlFor={`react-switch-new`}
>
<span className={`react-switch-button`} />
</label>
<h3 id='no'>No, thanks.</h3>
</div>
</div>
</>
);
How can I get each child element to have a functioning button, individual of the first and with independent states (in this case of 'true' and 'false').
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有两种常见的方法:
1。创建一个具有自己的单一
boolean
状态的Meat
子组件并将此状态保持在子级,独立于父组件
Switch
。如果需要,您可以通过回调函数onStatusChange
(例如使用useEffect()
)将status
数据传递回父级。您的
Switch
组件将如下所示:2.使用肉类类型数组而不是布尔状态来存储复选框值,然后在需要时将其转换为布尔值。使用这种方法,您不必创建新的
Meat
子组件。如果这能回答您的问题,请告诉我。
There are 2 common approaches:
1. Create a
Meat
child component with its own singleboolean
stateand keep this state at the child level, independent of the parent component
Switch
. You can pass thestatus
data back to parent if you want, via a callback functiononStatusChange
, for example with anuseEffect()
.And your
Switch
component will look like this:2. Use an array of meat types instead of a boolean state to store checkbox values, then convert it to boolean values later when needed. With this approach you don't have to create a new
Meat
child component.Let me know if this answers your question.
该代码稍微扩展并在下面“回答”。谢谢@kvooak 的帮助。事实证明,隐藏在 CSS 中以支持无响应按钮的复选框(实际上只是一些 CSS,没有“输入”功能)正是我所需要的。我使该复选框可见,添加到函数中以根据检查状态向数组添加或删除项目,并在末尾实时打印该数组,以证明应用程序正在保存/转储数组 allIngredients 中的正确项目。
The code is expanded upon slightly and 'answered' below. Thank you @kvooak for your help. Turns out that the checkbox that was hidden in CSS in favor of an unresponsive button (literally just some CSS and no 'input' functionality) was what I needed. I made that checkbox visible, added in a function to add or remove items based on checked status to and from an array, and printed that array at the end in realtime to prove that the app was holding/dumping the right items from array allIngredients.
您可以将状态设置为布尔数组,并使每个按钮对应于索引。例子:
You can set the state to be an array of booleans, and have each button correspond to the index. Example: