如何使用 useState 仅更新单击的元素?

发布于 2025-01-14 00:37:04 字数 1274 浏览 1 评论 0原文

我有一个包含步骤列表的章节列表。我试图在单击按钮时向章节添加步骤,但目前我正在向所有章节添加步骤。 我做错了什么?

  const dummy_chapters = [
    { id: 1, title: 'Chapter 1'},
    { id: 2, title: 'Chapter 2'},
    { id: 2, title: 'Chapter 3'},
  ]

const dummy_steps = [
    { id: 1, title: 'Step 1'},
    { id: 2, title: 'Step 2'},
  ]

  const [chapters, setChapters] = React.useState(dummy_chapters)
  const [steps, setSteps] = React.useState(dummy_steps)

  const addStep = () => {
    setSteps([
      ...steps,
      {
        id: 5,
        title: 'Another step'
      },
    ])
  }

  return (
      <List>
          {chapters.map((d, idx) => (
              <ChapterListItem    
                  secondaryAction={
                    <IconButton onClick={addStep}>
                      <MoreVertIcon/>
                    </IconButton>
                  }>
                  </ChapterListItem>
                <List component='div' disablePadding>
                  {steps?.map((d, idx) => (
                    <ListItemText primary={d.title}/>
                  ))} 
                </List> 
          ))}
      </List>
          <Icon onClick={addChapter} iconId='add' />
      </Box>
    </>
  )
}

I have a list of chapters that have a list of steps. I am trying to add a step to a chapter when clicking the button, but I am currently adding a step to all of the chapters instead.
What am I doing wrong?

  const dummy_chapters = [
    { id: 1, title: 'Chapter 1'},
    { id: 2, title: 'Chapter 2'},
    { id: 2, title: 'Chapter 3'},
  ]

const dummy_steps = [
    { id: 1, title: 'Step 1'},
    { id: 2, title: 'Step 2'},
  ]

  const [chapters, setChapters] = React.useState(dummy_chapters)
  const [steps, setSteps] = React.useState(dummy_steps)

  const addStep = () => {
    setSteps([
      ...steps,
      {
        id: 5,
        title: 'Another step'
      },
    ])
  }

  return (
      <List>
          {chapters.map((d, idx) => (
              <ChapterListItem    
                  secondaryAction={
                    <IconButton onClick={addStep}>
                      <MoreVertIcon/>
                    </IconButton>
                  }>
                  </ChapterListItem>
                <List component='div' disablePadding>
                  {steps?.map((d, idx) => (
                    <ListItemText primary={d.title}/>
                  ))} 
                </List> 
          ))}
      </List>
          <Icon onClick={addChapter} iconId='add' />
      </Box>
    </>
  )
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

土豪 2025-01-21 00:37:04

您可以在每个章节内添加steps键,而不是以不同的方式管理章节步骤。然后,要添加新步骤,您可以尝试如下所述的方法。

    const dummy_chapters = [{
        id: 1,
        title: 'Chapter 1',
        steps: [{
            id: 1,
            title: 'Step 1'
          },
          {
            id: 2,
            title: 'Step 2'
          },
        ]
      },
      {
        id: 2,
        title: 'Chapter 2',
        steps: []
      },
      {
        id: 2,
        title: 'Chapter 3',
        steps: []
      },
    ]

    const dummy_steps = [
        { id: 1, title: 'Another Chapter'},
      ]

     const [chapters, setChapters] = React.useState(dummy_chapters)

      const addStep = (chapterId) => {
        const updatedChapters = chapters?.map((chapter) => {
          if(chapter?.id === chapterId){
            chapter[steps] = [...chapter.steps, ...dummy_steps]
          }
          return chapter
        })
        setChapters(updatedChapters)
      }

      return (
          <List>
              {chapters.map((chapter, idx) => (
                  <ChapterListItem    
                      secondaryAction={
                        <IconButton onClick={() =>addStep(chapter?.id)}>
                          <MoreVertIcon/>
                        </IconButton>
                      }>
                      </ChapterListItem>
                    <List component='div' disablePadding>
                      {chapter?.steps?.map((step, idx) => (
                        <ListItemText primary={step.title}/>
                      ))} 
                    </List> 
              ))}
          </List>
              <Icon onClick={addChapter} iconId='add' />
          </Box>
        </>
      )
    }

Instead of managing chapters and steps differently you can add steps key inside of each chapters. Then for adding new steps you can try approach as mentioned below.

    const dummy_chapters = [{
        id: 1,
        title: 'Chapter 1',
        steps: [{
            id: 1,
            title: 'Step 1'
          },
          {
            id: 2,
            title: 'Step 2'
          },
        ]
      },
      {
        id: 2,
        title: 'Chapter 2',
        steps: []
      },
      {
        id: 2,
        title: 'Chapter 3',
        steps: []
      },
    ]

    const dummy_steps = [
        { id: 1, title: 'Another Chapter'},
      ]

     const [chapters, setChapters] = React.useState(dummy_chapters)

      const addStep = (chapterId) => {
        const updatedChapters = chapters?.map((chapter) => {
          if(chapter?.id === chapterId){
            chapter[steps] = [...chapter.steps, ...dummy_steps]
          }
          return chapter
        })
        setChapters(updatedChapters)
      }

      return (
          <List>
              {chapters.map((chapter, idx) => (
                  <ChapterListItem    
                      secondaryAction={
                        <IconButton onClick={() =>addStep(chapter?.id)}>
                          <MoreVertIcon/>
                        </IconButton>
                      }>
                      </ChapterListItem>
                    <List component='div' disablePadding>
                      {chapter?.steps?.map((step, idx) => (
                        <ListItemText primary={step.title}/>
                      ))} 
                    </List> 
              ))}
          </List>
              <Icon onClick={addChapter} iconId='add' />
          </Box>
        </>
      )
    }
纸短情长 2025-01-21 00:37:04

尝试在步骤对象中添加一个字段(例如 chapterId: 1),然后在递增之前添加一个条件以在递增之前检查章节 ID。
这样您就可以增加所需章节的步骤。

Try adding a field (like chapterId: 1 in your steps object, and then before incrementing add a condition to check chapter id before increment.
That way you can increment steps for the chapter you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文