Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

87
Visualizações
create a component for Axios that can be used in different places with different values

How can I create a component for Axios that I can use in different places with different values ​​?? I got stuck, what should I do? This is what I have achieved so far

thank you for Helping

import axios from "axios";


const Axios = (props) => {
    const [posttitle, postbody] = useState([]);
    const [postuserid, postid] = useState([]);
    const fetchData = () => {
        const { postbodyapi } = props.postbodyapi;
        const postuseridapi = "https://nba-players.herokuapp.com/players/james/lebron";
        const getbody = axios.get(postbodyapi);
        const getuseid = axios.get(postuseridapi);
        axios.all([getbody, getuseid]).then(axios.spread((...allData) => {
            const databody = allData[0].data.first_name;
            const datauseid = allData[1].config.url;
            postbody(databody);
            postid(datauseid);
        }))
    }

    useEffect(() => {
        fetchData()
    }, [])

    return (
        <div className="App">
            {posttitle}


            <img src={postuserid} alt="asd"/>
        </div>
    );
}
export default Axios;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You should create a custom hook.

Create a hook called for example useAxios and hold only the fetching method inside of it, and the return state from that hook should be just data. you can make it so it takes params like "URL, data, method", or make a few smaller hooks like useAxiosGet, useAxiosPost.

If you make a few smaller it will be easier to read and change something if needed.

Here is how I did it, an example of one specific Axios custom hook, use this for example to see how to build it.

useGetCar.js // custom axsios hook

import axios from 'axios';

const useGetCar = async (url, id) => {
    const result = await axios.post(url, {id: id});
    return result.data[0];
}

export default useGetCar


car.js // page component that displays data

import useGetCar from "@hooks/useGetCar";
let car_id = 1; // some that i send to api

// this function here is not exact from my code, 
//but I just wanted to provide you an example.

// I didn't include my original code because it is 
//from next.js app and I don't want to confuse u with that

async function getData() {
  let car = await useGetCar(`http://localhost/get_car.php`, car_id);
  return car;
}

Hope you understood what I'm saying, and I did not confuse you. Feel free to ask anything if you don't understand something clearly. Happy coding.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda