admin管理员组文章数量:1344534
I have created a dropdown menu using Material UI select. It says "Search By". When we click it, it gives us a list. When I select one of the options, I want to store the option and change the "Search By" to the option selected.
export default function UserSearchPage(){
const [criteria, setCriteria] = useState('');
const [searchItem, setSearchItem] = useState('');
return (
<div>
<div className='main-content'>
<Select
value = {criteria}
onChange={
value => { setCriteria(value);}
}
displayEmpty
>
<MenuItem disabled value="">
<em>Search By</em>
</MenuItem>
<MenuItem value={1}>First Name</MenuItem>
<MenuItem value={2}>Last Name</MenuItem>
<MenuItem value={3}>Phone Number</MenuItem>
<MenuItem value={4}>Email</MenuItem>
</Select>
</div>
)
</div>
);
}
Currently, the onChange gives me this error on value:
Argument of type 'ChangeEvent<{ name?: string | undefined; value: unknown; }>' is not assignable to parameter of type 'SetStateAction<string>'.
Type 'ChangeEvent<{ name?: string | undefined; value: unknown; }>' is not assignable to type '(prevState: string) => string'.
and if I use this:
onChange={event => setCriteria(event.target.value)}
I get:
Argument of type 'unknown' is not assignable to parameter of type 'SetStateAction<string>'.
Type 'unknown' is not assignable to type '(prevState: string) => string'.
I have created a dropdown menu using Material UI select. It says "Search By". When we click it, it gives us a list. When I select one of the options, I want to store the option and change the "Search By" to the option selected.
export default function UserSearchPage(){
const [criteria, setCriteria] = useState('');
const [searchItem, setSearchItem] = useState('');
return (
<div>
<div className='main-content'>
<Select
value = {criteria}
onChange={
value => { setCriteria(value);}
}
displayEmpty
>
<MenuItem disabled value="">
<em>Search By</em>
</MenuItem>
<MenuItem value={1}>First Name</MenuItem>
<MenuItem value={2}>Last Name</MenuItem>
<MenuItem value={3}>Phone Number</MenuItem>
<MenuItem value={4}>Email</MenuItem>
</Select>
</div>
)
</div>
);
}
Currently, the onChange gives me this error on value:
Argument of type 'ChangeEvent<{ name?: string | undefined; value: unknown; }>' is not assignable to parameter of type 'SetStateAction<string>'.
Type 'ChangeEvent<{ name?: string | undefined; value: unknown; }>' is not assignable to type '(prevState: string) => string'.
and if I use this:
onChange={event => setCriteria(event.target.value)}
I get:
Argument of type 'unknown' is not assignable to parameter of type 'SetStateAction<string>'.
Type 'unknown' is not assignable to type '(prevState: string) => string'.
Share
Improve this question
edited Mar 4, 2020 at 18:22
Ted
14.9k2 gold badges42 silver badges58 bronze badges
asked Mar 4, 2020 at 18:01
user12541823user12541823
2 Answers
Reset to default 7You have to assert the unknown
typed values before using them. If you're sure that the values from onChange
events will always be strings, then you can do onChange={event => setCriteria(event.target.value as string)}
and the piler should no longer plain.
define a type to your event argument. So it would be
onChange={event:React.ChangeEvent<{ value: unknown }> => setCriteria(event.target.value)}
本文标签: javascriptMaterial UI Select in TypescriptStack Overflow
版权声明:本文标题:javascript - Material UI Select in Typescript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743762657a2534664.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论