将默认值设置为更新表单中的反应选择

发布于 2025-02-09 10:02:56 字数 2249 浏览 3 评论 0原文

我很陌生反应,

我正在尝试使用React-Select编写更新表格 我很难设置默认选择

  let flight = useLocation();
  if (flight.state) {
    flight = flight.state.flightobj
  }

的是需要更新的飞行数据(从上一页获取)

(airlineOptions?.find(({ label }) => label === flight?.airline)

查找选项并为选择

问题格式化是因为它只是不设置默认值,我尝试做set set set set <代码> {label,value} 手动起作用。

第二个问题 onsubmit 中的prop选择由于某种原因不发射。 onsubmit形式明智的做法。我从代码中排除了onsubmit

const CreateFlightForm = () => {
  let flight = useLocation();
  if (flight.state) {
    flight = flight.state.flightobj
  }

  let [airlineOptions, setAirlineOptions] = useState()
  let [airline, setAirline] = useState()
  let [opt, setOpt] = useState(airlineOptions ? .find(({
    label
  }) => label === flight ? .airline));


  const handleSubmit = (e) => {
    e.preventDefault();
    // ... sends to db

  }

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


  // let defaultval = () => {
  //   let test = airlineOptions?.find(({ label }) => label === flight?.airline)
  //   // console.log(test);
  //   setOpt(test)
  // }


  let getAirlines = async() => {
    let response = await fetch(`/backend/airlines`)
    let data = await response.json()
    setAirlineOptions(data.map((airline) => ({
      value: airline.id,
      label: airline.name
    })))

  }


  return ( <
    div className = 'create' >
    <
    FormHeader headercheck = {
      flight
    }
    /> <
    form id = 'app_form'
    onSubmit = {
      (e) => handleSubmit(e)
    } >
    <
    label > Airline < /label>               <
    Select required id = 'airline'
    isClearable = {
      true
    }
    options = {
      airlineOptions
    }
    defaultValue = {
        opt
     }

    isSearchable = {
      true
    }
    onChange = {
      (e) => {
        if (e) setAirline(e.value)
      }
    }
            defaultValue = {opt}

    /> <
    input type = 'submit' > < /input>


    <
    /form>

    <
    /div>



  )

}

export default CreateFlightForm

I'm fairly new to react

I'm trying it write an update form with react-select
and i have a trouble to set the default selection

  let flight = useLocation();
  if (flight.state) {
    flight = flight.state.flightobj
  }

is the flight data that needed to be updated ( taken from previous page)

(airlineOptions?.find(({ label }) => label === flight?.airline)

Find the option and format it for select

The problem is that it just wont set the default, i tried to do set {label, value} manually and it works.

Second problem
onSubmit prop in the select not firing for some reason. onSubmit form wise does. I excluded onSubmit from the code .

const CreateFlightForm = () => {
  let flight = useLocation();
  if (flight.state) {
    flight = flight.state.flightobj
  }

  let [airlineOptions, setAirlineOptions] = useState()
  let [airline, setAirline] = useState()
  let [opt, setOpt] = useState(airlineOptions ? .find(({
    label
  }) => label === flight ? .airline));


  const handleSubmit = (e) => {
    e.preventDefault();
    // ... sends to db

  }

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


  // let defaultval = () => {
  //   let test = airlineOptions?.find(({ label }) => label === flight?.airline)
  //   // console.log(test);
  //   setOpt(test)
  // }


  let getAirlines = async() => {
    let response = await fetch(`/backend/airlines`)
    let data = await response.json()
    setAirlineOptions(data.map((airline) => ({
      value: airline.id,
      label: airline.name
    })))

  }


  return ( <
    div className = 'create' >
    <
    FormHeader headercheck = {
      flight
    }
    /> <
    form id = 'app_form'
    onSubmit = {
      (e) => handleSubmit(e)
    } >
    <
    label > Airline < /label>               <
    Select required id = 'airline'
    isClearable = {
      true
    }
    options = {
      airlineOptions
    }
    defaultValue = {
        opt
     }

    isSearchable = {
      true
    }
    onChange = {
      (e) => {
        if (e) setAirline(e.value)
      }
    }
            defaultValue = {opt}

    /> <
    input type = 'submit' > < /input>


    <
    /form>

    <
    /div>



  )

}

export default CreateFlightForm

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文