React - How to set onChange to change the props directly?

react hooks props change
react on props change
react set props value
react-trigger onchange
update component when props change
react prop object change
react function on prop change
onchange react

I have the following code that receives a variable and shows on the screen:

<input type="number" value={this.state.cpf} onChange={this.handleChange} name="name" />

handleChange(event) {
        this.setState({cpf: event.target.value});
      }

handleSignIn(event) {
        alert('A name was submitted: ' + this.state.cpf);
        event.preventDefault();
      }

I want to remove the handleChange, to change the direct props in onChange, I tried this but it did not work:

onChange={this.setState({ cpf: target.value })}

how do I change the onChange value with this.setState?

onChange here requires a function to be passed where it can inject the event as an argument. In your onChange, target.value will be undefined as there is not reference for value target.

Changing it to following should fix the issue.

onChange={(e) => {this.setState({ cpf: e.target.value})}

In this case the onChange event will have a function where it can pass the event object as e and you can perform your functionality.

React - How to set onChange to change the props directly?, preventDefault(); } I want to remove the handleChange, to change the direct props in onChange, I tried this but it did not work: onChange={this.setState({ cpf:​  A callback handler is passed down via React props and communicates up when the function is called. You have learned about React's event handler, inline event handler, and callback event handler and how to use them in buttons for their onClick events and in input fields for their onChange events.

Like @extempl pointed out, the handleChange event is not called because it's not binded to this so you either have to bind it by using.

There are several ways to solve this, depending on your code conventions, you could just bind the this keyword to the function like such:

onChange={this.handleChange.bind(this)}

or you could execute the function whenever onChange is triggered:

onChange={event () => this.handleChange(event)} // here i use the ES6 arrow function syntax.

NOTE: these are just examples, you could integrate this soulution into your own project

There are several other ways to do it as well, but these are some of the most common, I have provided a link to an article that explains, other methods as well:

https://medium.freecodecamp.org/react-binding-patterns-5-approaches-for-handling-this-92c651b5af56

Reacting to Prop Changes in a React Component, When building a component using React there is often a requirement to create a side effect when one of the component props changes. In addition, it's important to know that React's props are read only. There is no way in React to set props (even though it was possible in the past). After all, props are only used to pass data from one component to another component React, but only from parent to child components down the component tree. React Props vs. State

You can do this by using ES6 arrows functions :

onChange={(event) => {
               this.setState({cpf: event.target.value});
          }
}

It work fine... GOOD Luck !

How to update a component's prop in ReactJS, How to update a component's prop in ReactJS — oh yes, it's possible and the knowledge of the change required and the trigger to change it is only known to the child? All it needs to do is define a function onChange as I know this answer comes a little late but I recently faced a similar problem. I wanted to trigger an event on a nested component. I had a list with radio and check box type widgets (they were divs that behaved like checkboxes and/or radio buttons) and in some other place in the application, if someone closed a toolbox, I needed to uncheck one.

Simulate React On-Change On Controlled Components, Simulate React On-Change On Controlled Components. Anulesh Tiwari Manually trigger onChange when component is updated constructor(props) { We can set the value on input directly without triggering onChange . We only care about the state changes and React manages all events triggered on itself. Also if we don’t care about onChange event delegation we don’t need this approach. We can set the value on input directly without triggering onChange. But in situations where most of our logic relies on change events simulation.

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  I have set up redux in my React application, and props are being changed accordingly when the redux state is being changed, but I want to update my Component State when prop changes. I know there was a lifecycle function called willRecieveProps(), but I think is now deprecated. Any help would be useful. Thanks.

Lifting State Up – React, Often, several components need to reflect the same changing data. In this section, we will create a temperature calculator that calculates whether the water Just like the DOM <input> accepts both a value and an onChange prop, so can the  This is how React learns what should be displayed on the screen. React then updates the DOM to match the Clock’s render output. When the Clock output is inserted in the DOM, React calls the componentDidMount() lifecycle method. Inside it, the Clock component asks the browser to set up a timer to call the component’s tick() method once a second.

Comments
  • onChange takes a function
  • onChange={event => this.setState({ cpf: event.target.value })} should do the trick.
  • Your render method should always remain pure. Setting state in the render method t's a very bad practice and is more likely to have side effects
  • @lomse I try not to use the onChange method, but I can not type in form, do you know what happens? I want to change the value of the props without having to call a function, because I will use the updated value to make a post in an API.
  • Does your form input have a value property like this <input value={this.state.inputValue}>?
  • Setting state in the render method isn't a good practice since the render method is supposed to be pure
  • @lomse the state is being set on the execution of the event. Its the similar to assign a handler to the onChange event.
  • Still a bad practice stackoverflow.com/questions/35290245/…
  • Can you provide an better option for form manipulation here rather than just saying its a bad practice ? The example you have given is very specific to async call or functionalities that have side effects. As far as form manipulation is concern I would say we are fine with using setState here.