I got the user's state/province name and passed it into this array below. I'm trying to go into that specific state and fetch the cases and deaths as separate variables. However, in typescript, I having this error: Property 'state' does not exist on type 'never'.
Any ideas on how to use types to change the useState
and match it with the type of state
?
Array(53)0: {fips: "02", country: "US", state: "AK", county: null, level: "state", …}1: {fips: "01", country: "US", state: "AL", county: null, level: "state", …}2: {fips: "05", country: "US", state: "AR", county: null, level: "state", …}3: {fips: "04", country: "US", state: "AZ", county: null, level: "state", …}4: {fips: "06", country: "US", state: "CA", county: null, level: "state", …}5: {fips: "08", country: "US", state: "CO", county: null, level: "state", …}6: {fips: "09", country: "US", state: "CT", county: null, level: "state", …}7: {fips: "11", country: "US", state: "DC", county: null, level: "state", …}8: {fips: "10", country: "US", state: "DE", county: null, level: "state", …}9: {fips: "12", country: "US", state: "FL", county: null, level: "state", …}10: {fips: "13", country: "US", state: "GA", county: null, level: "state", …}// Goes into the first index of the array 0:actuals: {cases: 65944, deaths: 318, positiveTests: 107163, negativeTests: 2044164, contactTracers: 235, …}annotations: {cases: {…}, deaths: {…}, positiveTests: {…}, negativeTests: {…}, contactTracers: {…}, …}country: "US"county: nullfips: "02"lastUpdatedDate: "2021-04-21"lat: nulllevel: "state"locationId: "iso1:us#iso2:us-ak"long: nullmetrics: {testPositivityRatio: 0.030363378437363597, testPositivityRatioDetails: {…}, caseDensity: 22.164440621268295, contactTracerCapacityRatio: 0.28986784140969163, infectionRate: 0.968046978543, …}population: 731545riskLevels: {overall: 2, testPositivityRatio: 1, caseDensity: 2, contactTracerCapacityRatio: 1, infectionRate: 1, …}state: "AK"url: "https://covidactnow.org/us/alaska-ak"
Here is my current code below.
const [stateName, setStateName] = useState("AK"); const [data, setData] = useState([]); const [stateData, setStateData] = useState();useEffect(() => { axios.get('https://api.covidactnow.org/v2/states.json?apiKey={apiKey}') .then(response => response.data) .then(responseData => { setData(responseData) }) .catch(err => { console.log(err); }); }, []); useEffect(() => { setStateData(data.filter(i => i.state === stateName)[0]) //Error },[data, stateName])