Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

using map in useeffect

useEffect(async () => {
    try{ 
      let response = await axios.get(`https://swapi.co/api/people/`)
      let data = await response.json();
      let newState = data.map((e) => e); // map your state here
      setChars(newState); // and then update the state
      console.log(newState);
    } catch(error) {
       console.error(error.message);
    }
  },[]);
Comment

using map in useeffect

import React, { useState, useEffect } from 'react';
import axios from 'axios';
import './App.css';
import CharacterMap from './characterMap'

const App = () => {
  let [chars, setChars] = useState([]);
  useEffect(async () => {
    try{ 
      let response = await axios.get(`https://swapi.co/api/people/`)
      let data = await response.json();
      setChars(data);
    } catch(error) {
       console.error(error.message);
    }
  },[]);
 // If you want to access the updated state then use this.
  useEffect(() => {
     let newState = chars.map((e) => e); // map your state here
     setChars(newState); // and then update the state
     console.log(newState);
  },[getChars]);

  return (

    <div className="App">
      <CharacterMap info={chars} />
    </div>
  );
}
export default App;
Comment

PREVIOUS NEXT
Code Example
Javascript :: check if new user in firebase react 
Javascript :: lodash remove element from list 
Javascript :: you should not use switch outside a router react 
Javascript :: jquery today date 
Javascript :: javascript string contains substring 
Javascript :: check if string contains substring javascript 
Javascript :: javascript sort array of objects by key value 
Javascript :: javascript origin url 
Javascript :: svg to string javascript 
Javascript :: javascript to remove duplicates from an array 
Javascript :: node js catch any errors 
Javascript :: post data from api using jquery ajax 
Javascript :: jquery wp disable 
Javascript :: npm ERR! code EJSONPARSE 
Javascript :: merge array within an array 
Javascript :: javascript absolute path 
Javascript :: react router dom current path hook 
Javascript :: create an array of numbers 
Javascript :: javascript clear radio button 
Javascript :: jquery duplicate last table row 
Javascript :: Warning: Failed child context type: Invalid child context `virtualizedCell.cellKey` of type 
Javascript :: react yup password with number string and uppercase 
Javascript :: angular build aot vs jit 
Javascript :: javascript js isNumber 
Javascript :: click anchor tag using jquery 
Javascript :: vue.js slots 
Javascript :: export apk react native 
Javascript :: react useeffect avoid initial render 
Javascript :: safeareaview react native android 
Javascript :: javascript isalphanumeric 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =