简体   繁体   中英

Why can't I get it as an object (abonnes) from result Getelementbyid?

Why can't I get it as an object (abonnes) from result Getelementbyid? I get the element on JSON.

function Abonnes() {
  const [abonnes, setAbonnes] = useState();

  const getOneAbonnes = (id) => {
    Axios.get(`http://localhost:4000/api/getonea/${id}`).then((response) => {
      setAbonnes(response.data);
      console.log(abonnes); // undefined
    });
  };
}

because that's how your api is set it returns a JSON file, you can just parse it and you"re ready to go

Axios.get(`http://localhost:4000/api/getonea/${id}`).then((response)=>{
        
        setAbonnes(JSON.parse(response.data));                          
        console.log(abonnes); // this won't log the value you just set that is not how state works       
})

 useEffect(() => {
     console.log(abonnes)//this will log abonnes whenever abonnes value changes
  }, [abonnes])

function EditAbonnes() { 

    let history = useHistory();
    const { id } = useParams();
    /* alert(id); */

    const [nom, setNom] = useState('')
    const [prenom, setPrenom] = useState('')
    const [courriel, setCourriel] = useState('')

     
    const [abonnes, setAbonnes] = useState({
        courriel: "",
        id_Abonnes: "",
        nom: "",
        prenom: ""
        
    });
         
    useEffect (()=>{loadAbonnes();}, []);

    const loadAbonnes = async () =>{

        const result = await Axios.get(`http://localhost:4000/api/getonea/${id}`)

        console.log(result.data);
        /* i have an array with one object on my console /*

        setAbonnes(result.data);
                     
        console.log(abonnes);
        /* abonnes is empty   /*
        
        console.log(abonnes.nom); 
        /*empty string /* 
        
    }

return(        );} export default EditAbonnes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM