1

I want to assign response in Fruits array ones I fetched the data from Api using fetch...But I am getting empty array when console.log.. I am getting the response from Api but not able to assign it to fruits I am doing this way: .then(data => Fruits);

let Fruits = []



     useEffect(() => {
      
      const requestOptions = {
          method: 'POST',
          headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIwicm9sZSI6ImV4cGVydCJ9.6pYrAr_En0nl4N52oP1O7WRJA6PPFGCzUebauBIOEnc", },
          body: JSON.stringify({"dfdfdffd"})
      };
     
          fetch('https://d.com/audis/el/lt', requestOptions)
          .then(response => response.json())
          .then(data => Fruits);    

 
     }, []);
1
  • 2
    You most likely want to make Fruits state: const [fruits, setFruits] = useState([]);, and then set your state: .then(data => setFruits(data));. When you use state, your component will rerender (so then you can make your component use fruits to display content) Commented Mar 17, 2022 at 12:23

3 Answers 3

4

Use a state

const [fruits, setFruits] = useState([]);
useEffect(() => {
      
      const requestOptions = {
          method: 'POST',
          headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIwicm9sZSI6ImV4cGVydCJ9.6pYrAr_En0nl4N52oP1O7WRJA6PPFGCzUebauBIOEnc", },
          body: JSON.stringify({"firebase_id":"foCzPM8MgOtg1"})
      };
     
          fetch('https://d.com/audis/el/lt', requestOptions)
          .then(response => response.json())
          .then(data => setFruits(data);    
     }, []);
Sign up to request clarification or add additional context in comments.

Comments

4

You're not assigning data to anything. Try:

fetch('https://d.com/audis/el/lt', requestOptions)
          .then(response => response.json())
          .then(data => Fruits = data);   

Also, you should use a state do store the information.

const [fruits, setFruits] = useState();

fetch('https://d.com/audis/el/lt', requestOptions)
          .then(response => response.json())
          .then(data => setFruits(data));   

Comments

4

Maybe use a state?

    const [fruits, setFruits] = useState([])
    useEffect(() => {
      
      const requestOptions = {
          method: 'POST',
          headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIwicm9sZSI6ImV4cGVydCJ9.6pYrAr_En0nl4N52oP1O7WRJA6PPFGCzUebauBIOEnc", },
          body: JSON.stringify({"firebase_id":"foCzPM8MgOtg1"})
      };
     
          fetch('https://d.com/audis/el/lt', requestOptions)
          .then(response => response.json())
          .then(data => setFruits(data));    

       
  
 
     }, []);
     console.log(fruits);

1 Comment

Great minds think alike :)

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.