I am unable to consult API

Asked

Viewed 37 times

-1

How do I search for information within the api and display on the screen? It seems that the React does not recognize my function fetch

import React, { Component } from 'react'

class InputeAPI extends Component{
    constructor(){
        super()
        this.state={valor:'Seach for a country', buscaapi:[]}
        
        //agora consumiremos a API
        var isto=fetch('https://restcountries.eu/rest/v2/all').then(f=>f.json()).then(funcaoapi=>{console.log(funcaoapi)})

        this.consumir=this.consumir.bind(this)
    }
    
    consumir(){
        window.alert(this.state.valor)
        this.setState({buscaapi:this.funcaoapi})
        console.log(this.state.buscaapi)
    }
    render(){
        return(
            <div>
                <input id='inpu' onChange={(estado)=>this.setState({valor:estado.target.value})} type="text"value={this.state.valor}/>
                <button onClick={this.consumir}> procurar</button>
            </div>
        )
    }
}

export default InputeAPI

1 answer

-2

Try to make a proper method for the consummation of the api, do not use fetch/Axios inside the constructor of the Act.

Browser other questions tagged

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