【问题标题】:How to update individually buttons icons got from a same component in React如何单独更新从 React 中的同一组件获得的按钮图标
【发布时间】:2022-01-03 02:29:55
【问题描述】:

我有一个查看全部页面。该页面有一个包含用户数据的表格,其中包含可以执行的操作。该按钮有两个不同的图标。微调器图标和默认图标。我想在任务执行之前更改按钮的图标。

例如:在 M_1 的禁用/删除过程起作用之前显示微调器图标并显示锁定/垃圾箱图标。

我知道如何通过使用 useState 来更新一个按钮图标。当 1 行有 2 个按钮时,表示 2 个使用状态。所以 2 行意味着 4 个使用状态。但这并不实用。我想知道如何正确地做到这一点。

带按钮的表格:

查看所有页面:

import React, { useEffect, useState } from 'react'
import {
  CButton,
  CCard,
  CCardBody,
  CCardHeader,
  CCol,
  CRow,
  CSpinner,
  CTable,
  CTableBody,
  CTableDataCell,
  CTableHead,
  CTableHeaderCell,
  CTableRow,
} from '@coreui/react'
import CIcon from '@coreui/icons-react'
import { cilTrash, cilLockLocked } from '@coreui/icons'

const ViewAll = () => {
  const [usersData, setUsersData] = useState({})

  const [disabled, setDisabled] = useState(false)

  const handleLoadMembers = async () => {
    try {
      const _data = await fetch('http://localhost:4000/api/v1/member/list', {
        method: 'GET',
        headers: {
          'Content-Type': 'application/json',
          Authorization: 'Bearer ' + localStorage.getItem('token'),
        },
      })

      if (_data.status !== 200) {
        throw new Error()
      }

      const data = await _data.json()
      setUsersData(data.members)
    } catch (err) {
      console.error(err)
    }
  }

  const handleDelete = async (id) => {
    alert('clicked')
    try {
      const _data = await fetch('http://localhost:4000/api/v1/member/remove/' + id, {
        method: 'GET',
        headers: {
          'Content-Type': 'application/json',
          Authorization: 'Bearer ' + localStorage.getItem('token'),
        },
      })
      window.location.reload()

      console.log(_data)
    } catch (err) {
      console.error(err)
    }
  }

  useEffect(() => {
    handleLoadMembers()
  }, [])

  return (
    <CRow>
      <CCol xs={12}>
        <CCard className="mb-4">
          <CCardHeader>
            <strong>All Members</strong>
          </CCardHeader>

          {usersData.length > 0 ? (
            <CCardBody>
              <p className="text-medium-emphasis small">Here the all members of your community.</p>
              <CTable hover bordered>
                <CTableHead color="dark">
                  <CTableRow>
                    <CTableHeaderCell scope="col">Member ID</CTableHeaderCell>
                    <CTableHeaderCell scope="col">Name</CTableHeaderCell>
                    <CTableHeaderCell scope="col">Email</CTableHeaderCell>
                    <CTableHeaderCell scope="col">Actions</CTableHeaderCell>
                  </CTableRow>
                </CTableHead>
                <CTableBody>
                  {usersData.map((item, index) => {
                    return (
                      <CTableRow key={index}>
                        <CTableHeaderCell scope="row">M_{item.index}</CTableHeaderCell>
                        <CTableDataCell>{item.name}</CTableDataCell>
                        <CTableDataCell>{item.email}</CTableDataCell>
                        <CTableDataCell class="d-flex justify-content-evenly">
                          <CButton
                            color="danger"
                            size="sm"
                            disabled={disabled}
                            onClick={() => handleDelete(item._id)}
                          >
                            {disabled ? (
                              <CSpinner
                                component="span"
                                className="me-2"
                                size="sm"
                                aria-hidden="true"
                              />
                            ) : (
                              <CIcon icon={cilTrash} className="me-2" />
                            )}
                            Delete
                          </CButton>
                          <CButton
                            color="warning"
                            size="sm"
                            disabled={disabled}
                            onClick={() => handleDelete(item._id)}
                          >
                            {disabled ? (
                              <CSpinner
                                component="span"
                                className="me-2"
                                size="sm"
                                aria-hidden="true"
                              />
                            ) : (
                              <CIcon icon={cilLockLocked} className="me-2" />
                            )}
                            Disable
                          </CButton>
                        </CTableDataCell>
                      </CTableRow>
                    )
                  })}
                </CTableBody>
              </CTable>
            </CCardBody>
          ) : (
            'No data'
          )}
        </CCard>
      </CCol>
    </CRow>
  )
}

export default ViewAll

【问题讨论】:

    标签: javascript reactjs button icons state


    【解决方案1】:

    一个更好的想法是使用 react 和 redux 进行状态管理。

    解决问题的步骤:

    1. 进行 3 种操作类型(请求、成功、失败)。
    2. 在操作/sagas 文件中集成 API 调用。
    3. 当您从表中删除特定数据时,请求操作将分派到存储,此时您可以在请求缩减器类型中将特定数据/项目 ID 和加载标志设置为 true。
    4. 一旦操作派发成功,请确保在成功减速器类型中将加载设置为 false,在失败时类似。
    5. 在 props 中获取 id 和 loading 值并显示您想要显示的任何操作(删除/禁用微调器)。

    希望您能理解这些步骤。万事如意!!!!

    【讨论】:

      【解决方案2】:

      如何将 CButton 设置为组件并交出道具 false,true

      【讨论】:

      • 我试过了。但是 onClick 并没有按预期工作。
      • 我可以看到 btn 组件吗?
      猜你喜欢
      • 2023-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-30
      • 1970-01-01
      相关资源
      最近更新 更多