question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use event handler properly

See original GitHub issue

Hi, I’m sure this is a problem with my understanding, but I can’t figure out how to properly handle a change to the NumberFormat component (basically how to use my handler). I’m completely new to Reactjs, please play nicely with me 😃

The below works fine with an component: ` return ( <fieldset> <legend> Enter Phone Number </legend> <ValueField number={this.props.number} /> </fieldset> );

` But I can’t figure out how to do the same with the <NumberFormat> component:

return ( <fieldset> <legend> Enter Phone Number </legend> <NumberFormat isNumericString={true} format="(###) ###-####" mask=" " value={this.props.number} onValueChange={(values, e) => { const {formattedValue, value} = values; {handleChange(value)} } } /> <ValueField number={this.props.number} /> </fieldset> );

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
racingrebelcommented, Jul 3, 2018

You were using this function: onValueChange={(values, e) => this.handleChange(values, e)} To call this function: handleChange(e) { this.props.onNumberChange(e.target.value); }

As you can see the arguments do not line up. You are giving handleChange 2 arguments. As written, you are only using the first one ‘values’, but you are trying to read it as ‘e’ and event.

Here is a working version:

class PhoneInputForm extends React.Component {
  constructor(props) {
    super(props);
    this.handleNumberChange = this.handleNumberChange.bind(this);
    this.state = {
      number: '222'
    }
  }
  handleNumberChange(number) {
    this.setState({number: number}); 
  }
  render() {
    
    return (
    <div> 
      <h1>
        My Phone App
      </h1>
      <h2>
        <InputBox 
          number={this.state.number}
          onNumberChange = {this.handleNumberChange} />
        <ValueField number={this.state.number}/>
      </h2>
    </div>
    );
  }
}
class InputBox extends React.Component {
  constructor(props){
    super(props);
    this.handleChange = this.handleChange.bind(this);
  }
  handleChange(value) {
    this.props.onNumberChange(value);
  }
  render() {
    
    const number=this.props.number;
    
    if(number.length > 10) {
      return (
        <fieldset>
          <legend> Enter Phone Number </legend>
          <input
            value={this.props.number}
            onChange={this.handleChange}
            />
          <ValueField number={this.props.number} />
        </fieldset>
      );
    } else {
      return (
      <fieldset>
        <legend> Enter Phone Number </legend>
        <NumberFormat
          isNumericString={true}
          format="(###) ###-####"
          mask=" "
          value={this.props.number}
          onValueChange={values => this.handleChange(values.value)}
          />
        <ValueField number={this.props.number} />
      </fieldset>
    );
    }
  }
}
function ValueField(props) {
  
  if(isNaN(props.number) || props.number == '') {
    return <p> </p>;
  }
  
  return <p> +1{props.number} </p>;
  
}
ReactDOM.render(
  <PhoneInputForm />,
  document.getElementById('root')
);
0reactions
NoahLernercommented, Jul 3, 2018

Thanks for your help. I managed the custom format function. Here’s the final product if you’re interested.

https://gist.github.com/thewholuver94/ef43d718bf4e83104d08b46676d35756

Read more comments on GitHub >

github_iconTop Results From Across the Web

Event handling (overview) - Event reference - MDN Web Docs
The most flexible way to set an event handler on an element is to use the EventTarget.addEventListener method. This approach allows multiple ...
Read more >
Handling Events :: Eloquent JavaScript
Though we have ignored it so far, event handler functions are passed an argument: the event object. This object holds additional information about...
Read more >
Handling Events in JavaScript
There are three ways to assign an event handler: HTML event handler attribute, element's event handler property, and addEventListener() . Assign an event ......
Read more >
Handling JavaFX Events: Working with Event Handlers
To register a handler, use the addEventHandler() method. This method takes the event type and the handler as arguments. In Example 4-1, the...
Read more >
Understanding Event Handlers JS - Medium
To create a dynamic web application, it is inevitable to use events. The interaction with DOM elements in HTML and Javascript is dealt...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found