Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React JS: Pass event inside onChange drop down (Ant Design)

I have a drop down in my form (https://ant.design/components/select). In this select drop down I have the onChange to call a function. Inside 'onChange' I want to pass the event as a parameter to my function. The problem is: when the onChange occurs, only the selected value is passed, but I want the entire event.

Here is the code:

export default class MyForm extends Component {
    constructor() {
        super();

        this.handleOnChange = this.handleOnChange.bind(this);
    }

    handleOnChange = (event) => {
        console.log(event); // here I'm receiving only the value selected (1 or 2)
    }

    render() {
        render(
           <Form>
              <Select onChange={this.handleOnChange}>

                      <Option value="1">text 1</Option>
                      <Option value="2">text 2</Option>
              </Select>
           </Form>
        )
    }
}

In the console.log() I'm receiving only the selected value. Is there a way to pass the entire event object to the function handleOnChange()?

like image 990
Victor Avatar asked Aug 15 '17 00:08

Victor


People also ask

How do you add onChange event dropdown in React?

To handle the onChange event on a select element in React: Set the onChange prop on the select element. Keep the value of the selected option in a state variable. Every time the user changes the selected option, update the state variable.

Can onChange call two functions React?

the expected should be onchange event has to be call both the functions. You can only assign a handler to onChange once. When you use multiple assignments like that, the second one will overwrite the first one.

How to use ant design dropdown component in ReactJS?

We can use the following approach in ReactJS to use the Ant Design Dropdown Component. arrow: It is used to indicate whether the dropdown arrow should be visible or not. disabled: It is used to indicate whether the dropdown menu is disabled or not. getPopupContainer: It is used to set the container of the dropdown menu.

What is onchange event in react?

React onChange Events (With Examples) The onChange event in React detects when the value of an input element changes. Let’s dive into some common examples of how to use onChange in React.

How do you handle events in react JS?

Handling Events. Handling events with React elements is very similar to handling events on DOM elements. There are some syntactic differences: React events are named using camelCase, rather than lowercase. With JSX you pass a function as the event handler, rather than a string.

How do I rerender a React component after changing the value?

It seems like you might not have fully grasped the concept of React yet, so maybe "Thinking in React" helps. You have to store the selected value as state and update the state when the value changes. Updating the state will trigger a rerender of the component.


2 Answers

I found a solution. Just use: onSelect(), passing the value and the event.

handleOnChange = (value, event) => {
        ...code here        
}

    render() {
        render(
           <Form>
              <Select onSelect={(value, event) => this.handleOnChange(value, event)}>

                      <Option value="1">text 1</Option>
                      <Option value="2">text 2</Option>
              </Select>
           </Form>
        )
    }
like image 130
Victor Avatar answered Oct 23 '22 13:10

Victor


The Select component that you use is the one that handle the onChange and call your "outer" function.

What you can try is use the synthetic event variable inside your function, it might work:

handleOnChange = (selectedValue) => {
    console.log(selectedValue); // The value from the inner component
    console.log(event); // usually you have access to this variable
}
like image 31
Dekel Avatar answered Oct 23 '22 15:10

Dekel