Parsing error: Unexpected token React

Asked

Viewed 217 times

0

The error appears in this.converter = this.converter.bind(this);

My script:

import React, { Component } from 'react'

export default class conversor extends Component {
    
    constructor(props){
        super(props);

        this.state = {
            moedaA_valor: "",
            moedaB_valor: 0,
        }
    }

        this.converter = this.converter.bind(this);

    converter(){

        let de_para = `${this.props.moedaA}_${this.props.moedaB}`;
        let url = `https://api.currconv.com/api/v7/convert?q=${de_para}&compact`

        fetch(url)
        .then(res=>{

            return res.json()

        })
        .then(json=>{
            let cotacao = json[de_para].val;
            let moedaB_valor = ( parseFloat (this.state.moedaA_valor) * cotacao).toFixed(2)
            this.setState({moedaB_valor})
        })
    }
    
    render() {
        return(
            <div className="conversor">
                <h2>{this.props.moedaA} para {this.props.moedaB}</h2> 
                <input type="text" onChange={(event)=>(this.setState({moedaA_valor:event.target.value}))}></input>
                <input type="button" onClick= {this.converter} value="Converter"></input>
                <h2>{this.state.moedaB_valor}</h2>               
            </div>
        )
    }
}
  • Pedro, can you edit with the full code of your class? Furthermore, I think it is pq vc can not put commands in the middle of the class, the commands must exist inside a method.

1 answer

0

Puts this.converter = this.converter.bind(this); within the contructor (after super. Or alternatively use like this, Arrow Function directly to maintain the this in the class instance:

import React, { Component } from 'react'

export default class conversor extends Component {
    
    constructor(props){
        super(props);
        this.state = {
            moedaA_valor: "",
            moedaB_valor: 0,
        }
    }

    converter = () => {

        let de_para = `${this.props.moedaA}_${this.props.moedaB}`;
        let url = `https://api.currconv.com/api/v7/convert?q=${de_para}&compact`

        // etc...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.