Prevent a method to be called multiple times in React JS

RizkiDPrast

I am trying to experiment with reactjs multiple fields component by implementing Thinking in React but unable to figure out why method in the children called multiple times here.

var i = 0;
class SearchBar extends Component {
  constructor(props) {
    super(props);
    this.handleChange = this.handleChange.bind(this);
  }

  handleChange(key) {
    //this method is called multiple times during Render()
    console.log(i++);
    return function(e) {
      var value = key !== 'inStockOnly' ? e.target.value : e.target.checked;
      this.props.onUserInput(
        key,
        value
      );
    }.bind(this);
  }

  render() {    
    return (
      <form>
        <input type="search" value={this.props.filterText} placeholder="Search ..." ref={(input) => {input.focus();}} onChange={this.handleChange('filterText')} />
        <p>
        <input type="checkbox" checked={this.props.inStockOnly} onChange={this.handleChange('inStockOnly')} />
        {' '}
        Only show products in stock
        </p>
      </form>
    );
  }
}

class FilterableProducts extends Component {
constructor(props) {
  super(props);
  this.state = {
    filterText: '',
    inStockOnly: false
  };
  this.handleInput = this.handleInput.bind(this);
}
handleInput(key, value) {
/*var state = {};
  state[key] = value;  
  console.log(state);
  this.setState(state);*/

  //above is what I am trying to do later on but still stuck on why setState does not work
  //test
  this.setState({
    filterText: 'foo',
    inStockOnly: true
  });
}
  render() {
    return (      
      <SearchBar filterText={this.state.filterText} inStockOnly={this.state.inStockOnly} onUserInput={this.handleInput} />            
    );
  }
}
Update:

//updated handleChanged
handleChange(key, e) {
      var value = key !== 'inStockOnly' ? e.target.value : e.target.checked;
      this.props.onUserInput(
        key,
        value
      );
}

//updated handleInput
handleInput(key, value) {
  //var state = {};
  //state[key] = value;
  //this.setState(state);
  //console.log(state);
  //above is what I am trying to do
  //test
  this.setState({
    filterText: 'g',
    inStockOnly: true
  });
}
<!--i changed the event listener to this
  now its not called directly
-->
<input type="search" value={this.props.filterText} placeholder="Search ..." ref={(input) => {input.focus();}} onChange={(e)=>this.handleChange('filterText', e)} />

Any advice appreciated

Martin Dawson

Edit: That's correct, they are supposed to be called immediately because you are returning a function which binds to the onChange for when it fires. The inner function will only be called onChange. You will need to pass the key in though.

Second Edit:

handleInput(event) {
  this.setState({
    filterText: event.target.value,
    inStockOnly: true
  });
}
<input type="search" value={this.state.filterText} placeholder="Search ..." ref={(input) => {input.focus();}} onChange={this.handleInput} />

Original:

handleChange(key) {
    //this method is called multiple times during Render()
    console.log(i++);
    return function(key, event) {
      var value = key !== 'inStockOnly' ? event.target.value : event.target.checked;
      this.props.onUserInput(
        key,
        value
      );
    }.bind(this);
}

Event will be passed as the last parameter when it fires.

What isn't working about the setState? You are using it correctly in handleInput.

The commented out setState won't work though. You should only create state in the constructor and also do note though that the console.log() after you set the state may still be empty though because setState is asynchronous for performance and may not be updated until later.

var state = {};
state[key] = value;  
console.log(state);
this.setState(state);*/

You will want componentDidUpdate instead and perform the console.log in there or use the callback of setState, i.e this.setState({state[key]: value}, function() {console.log(this.state)}); which will always give the correct state.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Compute method is called multiple times?

From Dev

Compute method is called multiple times?

From Dev

Sum method called multiple times in getview

From Dev

Netty decoder method called multiple times

From Dev

AngularJS event method called multiple times

From Dev

Sum method called multiple times in getview

From Dev

Prevent js function running multiple times

From Dev

Prevent js function running multiple times

From Java

Can Mockito capture arguments of a method called multiple times?

From Dev

Why is my DataPoints method being called multiple times?

From Dev

If a method is inside a linq query would it be called multiple times?

From Dev

the paint method is called multiple times... how to restrict that?

From Dev

CakePHP lean controller method that gets called multiple times per minute

From Dev

Async method in controller is called multiple times when an exception is caught

From Dev

kReachabilityChangedNotification is called multiple times

From Dev

MouseListener called multiple times

From Dev

MouseListener called multiple times

From Dev

OnDestroy() called multiple times

From Dev

kReachabilityChangedNotification is called multiple times

From Dev

observeValueForKeyPath called multiple times

From Dev

React component render is called multiple times when pushing new URL

From Dev

React JS component renders multiple times in Meteor

From Dev

Count number of times a method is called?

From Dev

loadview method is called several times

From Dev

Why NSApplicationDelegate method openFiles: is being called multiple times on a multiple drag to the dock icon?

From Dev

How to prevent method to be called twice?

From Dev

Why is this function called multiple times?

From Dev

Onclick is being called multiple times

From Dev

OnAuthorization being called multiple times

Related Related

  1. 1

    Compute method is called multiple times?

  2. 2

    Compute method is called multiple times?

  3. 3

    Sum method called multiple times in getview

  4. 4

    Netty decoder method called multiple times

  5. 5

    AngularJS event method called multiple times

  6. 6

    Sum method called multiple times in getview

  7. 7

    Prevent js function running multiple times

  8. 8

    Prevent js function running multiple times

  9. 9

    Can Mockito capture arguments of a method called multiple times?

  10. 10

    Why is my DataPoints method being called multiple times?

  11. 11

    If a method is inside a linq query would it be called multiple times?

  12. 12

    the paint method is called multiple times... how to restrict that?

  13. 13

    CakePHP lean controller method that gets called multiple times per minute

  14. 14

    Async method in controller is called multiple times when an exception is caught

  15. 15

    kReachabilityChangedNotification is called multiple times

  16. 16

    MouseListener called multiple times

  17. 17

    MouseListener called multiple times

  18. 18

    OnDestroy() called multiple times

  19. 19

    kReachabilityChangedNotification is called multiple times

  20. 20

    observeValueForKeyPath called multiple times

  21. 21

    React component render is called multiple times when pushing new URL

  22. 22

    React JS component renders multiple times in Meteor

  23. 23

    Count number of times a method is called?

  24. 24

    loadview method is called several times

  25. 25

    Why NSApplicationDelegate method openFiles: is being called multiple times on a multiple drag to the dock icon?

  26. 26

    How to prevent method to be called twice?

  27. 27

    Why is this function called multiple times?

  28. 28

    Onclick is being called multiple times

  29. 29

    OnAuthorization being called multiple times

HotTag

Archive