1

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

<input type="number" name="quantity" 
  min="1" 
  step="1"
  value={this.state.userCount}
  onChange={this.handleUserCountChange}
/>

How to set the default value to 5 for the numeric field?

1
  • 1
    You didn't attach your code. Commented Oct 23, 2016 at 9:39

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

1

in your constructor you can set the state.

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

Comments

0

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
   };
 }
}

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.