0

I would like to return a single variable on my website that is founded in my API REST. I'm using Axios to integrate my backend to my frontend.

import api from '../../api';

async function handleBooks(e){
    e.preventDefault();
    
    const data = {
        title,
        reservation,
        rent,
        devolution
    }; 

    try{
        const response = await api.get('books', data);
        console.log(response); //it is working!
    }catch(err){
        console.log('!response');
    }
}

 return(
    //I would like to return one of those variables upside mentioned, for example: 'title'.
    <div>{response.data.title}<div/>
 );

2 Answers 2

1

Set response as React state:

import api from '../../api';
import React, {useState} from 'react

async function handleBooks(e){
    e.preventDefault();
    const [data, setData] = useState([])
    
    const data = {
        title,
        reservation,
        rent,
        devolution
    }; 

    try{
        const response = await api.get('books', data);
        setData(response.data)
        console.log(response); //it is working!
    }catch(err){
        console.log('!response');
    }
}

 return(
    //I would like to return one of those variables upside mentioned, for example: 'title'.
    <div>{data && data.title}<div/>
 );
Sign up to request clarification or add additional context in comments.

4 Comments

Must I change it as a render function? As this function 'handleBooks' is inside a 'onSubmit', I'm receiving the data after the click, so then I cannot load <div>{data && data.title}</div>
yes you need to change it in the render function render() { return( <div>{data && data.title}<div/>); }
Do you know how could I put all of these states as states on 'export default Class Books(){}' Instead of 'export default function Books()" as is wrote for the second model? const [title, setTitle] = useState(''); const [reservation, setReservation] = useState(0); const [rent, setRent] = useState(0); const [devolution, setDevolution] = useState(0); const [data, setData] = useState([]);
The useState() variable can only be used in a React component, so move it out of the handleBooks() function, into its parent component. Also, if it were to work, the jsx would not be able to reach the data variable because it belongs to a child function of the component. (because of closures)
0

Answer:

+ const [datas, setData] = useState([]);
 + try{
            const response = await api.get('books', data);
            setData(response.data);
            console.log(response.data);
        }
    <ul>
        {datas.map(data => (
            <li key={data.id}>
            <p>{data.title}</p>
            <p>{data.rent}</p>
            <p>{data.devolution}</p>
            <p>{data.reservation}</p>
            </li>
         ))}
</ul>

Comments

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.