Can't get the actual value of a input in react

I was developing a react component to get a value inside a input and automatically show it in a tag, using refs.

All works fine, but the value shown is the previous value.

I really don't now how to fix this. I using the onChange event in the input to change the state of what will be shown, it is clear that the present value is not taken, but rather the previous value

class Conversor extends Component {
    constructor(props){
        super(props)

        this.state = {
            value: null
        }

        this.output = this.output.bind(this)
    }

    output(){

        console.log(this.state)

        this.refs.output.innerHTML = this.state.value
    }

    render() {
        return (
            <div>
                <h2>{this.state.inputValue}</h2>
                <input ref="input" type="text" onChange={() => {this.setState({ value: this.refs.input.value }); this.output()}}/>
                <label ref="output"></label>
            </div>
        );
    }
}

If i put the value "Hello World" in the input, the value shown is "Hello Worl", when it's have to be the "Hello World"

You can use event to do this and no need of output() function.

class Conversor extends Component {
    constructor(props){
        super(props)

        this.state = {
            value: null
        }
    }

    render() {
        return (
            <div>
                <h2>{this.state.inputValue}</h2>
                <input ref="input" type="text" onChange={(e) => {this.setState({ value: e.target.value });}}/>
                <label ref="output">{this.state.value}</label>
            </div>
        );
    }
}

Can, Can was the leading avant-garde rock group of the 70s. Can experimented with noise, synthesizers, non-traditional music, cut-and-paste techniques, and, most� Can definition, to be able to; have the ability, power, or skill to: She can solve the problem easily, I'm sure. See more.

The best way to achieve your goal is not using the refs. Here is how you do it

class Conversor extends Component {
    constructor(props){
        super(props)

        this.state = {};
    }

    handleChange = (e) => {
      const { id, value } = e.target;
      this.setState({
        [id]: value
      })
    }

    render() {
      const { name, anotherName } = this.state;
        return (
            <div>
                <h2>{name}</h2>
                <input id="name"  name="name" type="text" onChange={this.handleChange}/>

                <h2>{anotherName}</h2>
                <input id="anotherName"  name="anotherName" type="text" onChange={this.handleChange}/>
            </div>
        );
    }
}

If you still want to use the refs then do the following,

class Conversor extends Component {
    constructor(props){
        super(props)

        this.state = {
            value: null
        }
    }

    output = (e) =>{
        this.setState({value: e.target.value }, () => {
        this.refs.output.innerHTML = this.state.value
        })
    }

    render() {
        return (
            <div>
                <input ref="input" type="text" onChange={this.output}/>
                <label ref="output"></label>
            </div>
        );
    }
}

The Official CAN / Spoon Records Website, Can may refer to: Contents. 1 Containers; 2 Music; 3 Other; 4 See also. Containers[edit]. Aluminum can � Drink can � Oil can � Steel and tin cans � Trash can� Define can. can synonyms, can pronunciation, can translation, English dictionary definition of can. to be able to, have the power or skill to: I can take a bus to the

You don't need to bind your input handler function at all. Instead of doing that, just use an arrow function like _handleInputTextChange . Check this out:

import React, { Component } from 'react';
class InputTextHandler extends Component {
   constructor(props){
       super(props)
       this.state = {
           inputValue: ''
       }
   }

   _handleInputTextChange = e => {
       const inputValue = e.target.value;
       this.setState({inputValue})
       console.log(inputValue)
   }

   render() {
       return (
           <div>
               <input
                    type="text"
                    onChange={this._handleInputTextChange}/>
           </div>
       );
   }
}

export default InputTextHandler;

Can, VerbEdit. can (third-person singular simple present can, present participle -, simple past could, past participle (obsolete except in adjectival use) couth). can definition: 1. to be able to: 2. used to say that you can and will do something: 3. to be allowed to: . Learn more.

Two things: grab the event value in the onChange method, and pass the this.output method as the second argument to setState which fires after the state has been updated which is not a synchronous operation.

render() {
    return (
        <div>
            <h2>{this.state.inputValue}</h2>
            <input ref="input" type="text" onChange={event => {this.setState({ value:event.target.value }, this.output)}}/>
            <label ref="output"></label>
        </div>
    );
}

Try it here!

Can (band), 1.4Used to indicate that something is typically the case. 'antique clocks can seem out of place in modern homes'. More example sentences. 110 synonyms of can from the Merriam-Webster Thesaurus, plus 97 related words, definitions, and antonyms. Find another word for can. Can: to bring (as an action or operation) to an immediate end.

can, can noun [C] (CONTAINER) � can verb [T] (CONTAIN) � can modal verb (ABLE) � can modal verb (PERMIT) � can modal verb (BE POSSIBLE) � can modal verb ( OFFER). HANGZHOU, China, Aug. 31, 2020 (GLOBE NEWSWIRE) -- Canaan Inc. (NASDAQ: CAN) ("Canaan" or the "Company"), a leading high-performance computing solutions provider, today announced its unaudited

Can, can container � 1. countable noun A can is a metal container in which something such as food, drink, or paint is put. � 2. verb [usually passive] When food or drink� Looking for online definition of CAN or what CAN stands for? CAN is listed in the World's largest and most authoritative dictionary database of abbreviations and acronyms The Free Dictionary

CAN, Can 'Vitamin C' is currently featured in the film 'Inherent Vice' directed by Paul Thomas Duration: 3:33 Posted: Feb 6, 2015 can (someone) To summarily dismiss or oust someone from employment; to fire someone. Management promptly canned the new accountant after his miscalculation cost the company hundreds of thousands of dollars. They'll can me if they ever find out I came into work drunk yesterday.

Comments
  • thanks man, works fine. but do you know why it's have a diferent result when i, for example, put other function in the onChange atribute?
  • The problem in your case is setState() is an asynchronus function, when you call output() function the state might not be updated. What you can do for this is pass your output() function as a callback function to the setState function. setState({value: e.target.value},()=>{this.output()})