Artifs
Artifs

Reputation: 21

How i can converting numbers from input to int in react js

import React, { Component } from 'react'
export default class Ccomponent extends Component {
    constructor(props) {
        super(props)
        this.state = {
            first: 0,
            second: 0,
            Otv: 0
        };
        this.firstChange = this.firstChange.bind(this);
        this.secondChange = this.secondChange.bind(this);
        this.Otvet = this.Otvet.bind(this);
        this.ConvertToInt = this.ConvertToInt.bind(this);
    }
firstChange(event){
    this.setState({
        first: event.target.value
    });
    this.Otvet()
}
secondChange(event){
    this.setState({
        second: event.target.value
    });
    this.Otvet()
}
Otvet(event){   
    this.setState({
        Otv: this.state.first+this.state.second
    });
    this.ConvertToInt();
}
ConvertToInt(event){
    var a = this.state.first;
    var b = this.state.second;
    let v = a+b //i can try use parceInt, error - 'parceInt' is not defined  no-undef
    console.log(v);
}
    render() {
        console.log(this)
        return(
            <div>
               <input type='number' value = {this.state.first} onChange={this.firstChange}/>
               <input type='number' value = {this.state.second} onChange={this.secondChange}/>
               <h3>Summa: {this.state.first}+{this.state.second}</h3>
               <h1>Otvet: {this.state.Otv}</h1>
            </div>
        )
}}

My input send value to object in string format, how i can convert str to int, if parceInt doesn't worked. Maybe there is a way to get the value in the form int? Or maybe react have internal function for converting string to integer?

Upvotes: 0

Views: 7832

Answers (1)

AwesomeDude3247
AwesomeDude3247

Reputation: 98

parceInt() isn't spelled correctly. Small typo 😅. It's spelled like parseInt() Try that out.

For example,

let str = "123"
let num = parseInt(str)
console.log(num) // 123

Upvotes: 1

Related Questions