如何使用从父组件传递的Prop中的React中的选择下拉迭代?

发布于 2025-02-10 07:34:05 字数 2392 浏览 3 评论 0原文

我正在模态中使用来自父组件的道具,但我不确定如何在选择下的下拉下显示。我看到正在重复使用固执的“代理”值,但是我该如何使该值动态地显示我收到的道具的值?我在这里想念什么?

反应模态组件

import React, { useState, useEffect } from 'react'
import Button from '@/components/Button.jsx'
import Loading from '@/components/Loading.jsx'
import Modal from '@/components/Modal.jsx'

/**
 * Handles displaying the capabilities modal for creating new capabilities or updating exiting ones.
 * @component
 */ 
export default function AddCapabilitiesModal({ profile, onClose }) {
  const [error, setError] = useState(null)
  const [loading, setLoading] = useState(false)
  const [agents, setAgents] = useState([])
  const [profiles, setProfiles] = useState([])

  useEffect(() => {
    if (profile) {
      console.log(profile);
      setAgents(profile.map((f) => ({ label: f.agent, value: f.agent })))
      setLoading(false);
    }
      }, [])

    /**
   * Erases information of text field variables and closes modal.
   */
     const closeModal = () => {
      setError(false)
      setLoading(false)
      onClose()
    }

    const dummyClick = () => {
      console.log('click')
    }

  return (
    <div>
    <Modal>
        {error && (
          <label className='high'>
            Error: Please confirm inputs and try again.
          </label>
        )}
        <div className='modal-title'>
          <label>EDIT STATUS</label>
        </div>
        <br />
        <Loading text='Saving Test...' visible={loading} />
        {!loading && (
        <div id='addCapSelectInput' className='sub-filters'>
          <div className='lrow filter-options group light-grey-bg'>

          <select>
          {agents.map((agent) => (
            <option key={agent} value={agent}>
            Agent
          </option>
            ))} 
          </select>

          </div>
        </div>
        )}
        <div className='modal-row top-horizontal-divider'>
          <Button onClick={closeModal} className='modal-cancel'>
            Cancel
          </Button>
          <Button
            onClick={dummyClick}
            className='modal-submit'
          >
            Submit
          </Button>
        </div>
      </Modal>
    </div>
  )
}

I am using props from a parent component in a modal and I am unsure of how to make it show in a select dropdown dynamically. I see that the stubbed hardcoded 'Agent' value is being repeated but how do I actually make that dynamically show the values from the props I am receiving? What am I missing here?

React Modal Component

import React, { useState, useEffect } from 'react'
import Button from '@/components/Button.jsx'
import Loading from '@/components/Loading.jsx'
import Modal from '@/components/Modal.jsx'

/**
 * Handles displaying the capabilities modal for creating new capabilities or updating exiting ones.
 * @component
 */ 
export default function AddCapabilitiesModal({ profile, onClose }) {
  const [error, setError] = useState(null)
  const [loading, setLoading] = useState(false)
  const [agents, setAgents] = useState([])
  const [profiles, setProfiles] = useState([])

  useEffect(() => {
    if (profile) {
      console.log(profile);
      setAgents(profile.map((f) => ({ label: f.agent, value: f.agent })))
      setLoading(false);
    }
      }, [])

    /**
   * Erases information of text field variables and closes modal.
   */
     const closeModal = () => {
      setError(false)
      setLoading(false)
      onClose()
    }

    const dummyClick = () => {
      console.log('click')
    }

  return (
    <div>
    <Modal>
        {error && (
          <label className='high'>
            Error: Please confirm inputs and try again.
          </label>
        )}
        <div className='modal-title'>
          <label>EDIT STATUS</label>
        </div>
        <br />
        <Loading text='Saving Test...' visible={loading} />
        {!loading && (
        <div id='addCapSelectInput' className='sub-filters'>
          <div className='lrow filter-options group light-grey-bg'>

          <select>
          {agents.map((agent) => (
            <option key={agent} value={agent}>
            Agent
          </option>
            ))} 
          </select>

          </div>
        </div>
        )}
        <div className='modal-row top-horizontal-divider'>
          <Button onClick={closeModal} className='modal-cancel'>
            Cancel
          </Button>
          <Button
            onClick={dummyClick}
            className='modal-submit'
          >
            Submit
          </Button>
        </div>
      </Modal>
    </div>
  )
}

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

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

发布评论

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

评论(1

爺獨霸怡葒院 2025-02-17 07:34:05

正如我可以看到的那样,状态中的代理是包含标签和值密钥的对象的列表。因此,您应该将渲染部分重写为这样的部分:

{
     agents.map((agent) => (
       <option key={agent.label} value={agent.value}>
         {agent.label}
        </option>
     ))
   }

As I can see your agent in state is a list of objects containing label and value keys. So you should just rewrite render part to something like this:

{
     agents.map((agent) => (
       <option key={agent.label} value={agent.value}>
         {agent.label}
        </option>
     ))
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文