How to trigger onChange of a select control programmatically in React 17.0.2

Okay, the reason I am asking this is because I can’t find any modern answers … Read more How to trigger onChange of a select control programmatically in React 17.0.2

Okay, the reason I am asking this is because I can’t find any modern answers to this commonly asked question.

So I have a component in which I have this select control:

const [selectedProject, setSelectedProject] = useState('');

return (
    <Container>
        <Stack gap={16}>
            <div>
                <Stack gap={0}>
                    <Text type="baseBold">Project</Text>
                    {
                        <Select
                            name="select-list"
                            value={selectedProject}
                            placeholder={'Please select business area'}
                            onChange={(value) => {
                                setSelectedProject(value);
                                doGetSomething(value);
                                doGetPromotedSomething(value);
                            }}
                            (...)

It works fine when a user interacts with the select control, i.e. selects an option manually. However, I want to trigger onChange MYSELF programmatically. How do I do that? This does NOT trigger onChange:

setSelectedProject(selectedProject);

So how do I trigger it? Please bear in mind this is a modern version of react.

Source: React – Stack Overflow


Categories: NewsTags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *