开发者问题收集

ReactJS TypeError:无法读取未定义的属性“value”

2021-08-16
75

访问此字段时抛出 ReactJS TypeError: Cannot read property 'value' of undefined

alert(event.target.value)

import React , { useState }from 'react';
import {DashboardLayout} from '../components/Layout';
import Select from 'react-select'

const options = [
    { value: 'ami-abc*', label: 'ami-abc' },
    { value: 'ami-xyz*', label: 'ami-xyz' },
]

const DiscoverAMIPage = () => {
    return (
        <DashboardLayout>
            <h2>Discovered AMI</h2>
            <Select
                onChange={handleChange}
                options={options}
            />
        </DashboardLayout>
    )
}

function handleChange(event) {
    alert(event.target.value)
}
export default DiscoverAMIPage;
1个回答

react-select 中的 onChange 返回 value 而不是 event https://react-select.com/props

function handleChange(value) {
  alert(value)
}
Viet
2021-08-16