2

I want to change the arrow style function (at onChange) in this react component into an ordinary function. I'm a beginner and for me it is helpful to see both versions side by side since I have a hard time getting the hang for the new arrow function syntax.

It should be possible but how would the could below look like using "ordinary" functions?

import React from "react";

function Input1(props) {
  //console.log(props)
  return (
    <div>
      <input
        onChange={event => props.handleChange("email", event.target.value)}
      />
    </div>
  );
}

export default Input1;
2
  • 1
    function (event) { return props.handleChange("email", event.target.value); } ? Commented Sep 25, 2019 at 13:43
  • If you just want a multi-line function, just using event => { /* multi-line of code here */ } would work. Commented Sep 25, 2019 at 14:12

4 Answers 4

5
import React from "react";

function Input1(props) {
  //console.log(props)

  function onChange(event) {
    props.handleChange("email", event.target.value)
  }

  return (
    <div>
      <input
        onChange={onChange}
      />
    </div>
  );
}

export default Input1;
Sign up to request clarification or add additional context in comments.

Comments

1
 <div>
  <input
    onChange={function(event) {
          props.handleChange("email", event.target.value)
         }
   }
  />
</div>

Comments

1
import React from "react";

function Input1(props) {

  function changeHandler(event) {
     props.handleChange("email", event.target.value)
  }

  //console.log(props)
  return (
    <div>
      <input
        onChange={changeHandler}
      />
    </div>
  );
}

export default Input1;

Comments

-1
import React from "react";

class Input1 extends React.Component {
 constructor(props) {
    super(props);
    //need to bind in case of using function decleration
    this.handleChange = this.handleChange.bind(this);
  }

 handleChange(event){
   props.handleChange("email", event.target.value);
  }

 render() {
   return (
    <div>
      <input
        onChange={this.handleChange}
      />
    </div>
   )
 }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.