1

I have one input which i want to update using ref only(not using setState). constructor(props) { super(props);

    this.Increase = this.Increase.bind(this)
    this.textInput = React.createRef();
    this.state = {
        inputVal: -1
    };
}

<div>
<input type="number" ref={textInput} placeholder="count"/>
<span onClick={this.Increase}>Increase
</span>
</div>

and i have one function where i want to use to increase input value

Increase(event){
//access ref here and update input value using ref
}

Any help will be appreciated.

Thanks

2 Answers 2

3

It's just like changing a mere normal field value. You could just do :

Increase(event) {
    event.preventDefault();
    this.textInput.current.value += 1;
}
Sign up to request clarification or add additional context in comments.

2 Comments

i want to use ref
You're actually using ref (the one you created). Or are you talking about the hook??
1

The first step to fix this problem is to pass the ref properly:

<div>
    <input type="number" ref={this.textInput} placeholder="count"/>
    <span onClick={this.Increase}>Increase</span>
</div>

then you should add this Increase function:

Increase(event) {
    event.preventDefault();
    this.textInput.current.value = "2" //everything you want;
}

1 Comment

also you can visit this jsfiddle.net/9bgxr372/12

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.