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: null
fips: "02"
lastUpdatedDate: "2021-04-21"
lat: null
level: "state"
locationId: "iso1:us#iso2:us-ak"
long: null
metrics: {testPositivityRatio: 0.030363378437363597, testPositivityRatioDetails: {…}, caseDensity: 22.164440621268295, contactTracerCapacityRatio: 0.28986784140969163, infectionRate: 0.968046978543, …}
population: 731545
riskLevels: {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])
You can do it like this
const [stateData, setStateData] = useState<any>();
I used any so you can understand where to place your type. You should of course define your own type.
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.