Skip to content Skip to sidebar Skip to footer

How To Set Default Value To Numeric Input Field In Reactjs?

I am working on a sample reactjs form, which has a input number field as shown below:

Solution 1:

You would need to set the initial state of userCount to 5 check the docs : https://facebook.github.io/react/tutorial/tutorial.html#an-interactive-component


Solution 2:

in your constructor you can set the state.

constructor(props) {
  super(props);
  this.state = {
    userCount: 5
  };
}  

Solution 3:

You have set the value of that numeric input to the component state:

this.state.userCount

When this form is initially rendered, it will populate the input with the value stored in this.state.userCount.

Like other posters have mentioned, you just have to set the the initial state:

React createClass syntax:

var TestForm = React.createClass({
  getInitialState() {
    return {
      userCount: 5
    };
  },
});


ES6/ES2015 class syntax:

class TestForm extends React.Component {
 constructor(props) {
   super(props);
   this.state = {
    userCount: 5
   };
 }
}

Post a Comment for "How To Set Default Value To Numeric Input Field In Reactjs?"