I use react hooks to update, but notice error when setState.
Argument of type '{ alertRules: any; }' is not assignable to parameter of type 'SetStateAction'. Object literal may only specify known properties, and 'alertRules' does not exist in type 'SetStateAction'.ts(2345)
Here is my code.
import React, { useState, useEffect } from 'react';
import { FieldArray } from 'redux-form';
import { CoordinateSelect } from '~/fields';
import lodash from 'lodash';
import { connect } from 'react-redux';
import { filterDispatchers } from '~/actions';
import t from '~/locale';
interface Props {
getAlertRules(o: object): Promise<any>;
}
type Alert = {
...
}
const connector = connect(
null,
filterDispatchers('getAlertRules'),
);
const AlertRuleForm = (props: Props) => {
const [alertRules, setAlertRules] = useState<Alert[]>([]);
useEffect(() => {
fetchData();
}, []);
const fetchData = async () => {
const actionResult = await props.getAlertRules({ limit: -1 });
const alertRules = lodash.get(actionResult, 'response.alertRules', []);
setAlertRules({ alertRules }); //Error form here
};
const groupedRules = lodash.groupBy(alertRules, 'resourceType');
const levelTypes = lodash.uniq(alertRules.map((alert: Alert) => alert.level));
return (
<FieldArray
name="alertRules"
component={CoordinateSelect}
label={t('å‘Šè¦è§„则')}
firstOptions={lodash.keys(groupedRules)}
secondOptions={groupedRules}
thirdOptions={levelTypes}
required
/>
);
};
export default connector(AlertRuleForm);
the error is when set state
Argument of type '{ alertRules: any; }' is not assignable to parameter of type 'SetStateAction'. Object literal may only specify known properties, and 'alertRules' does not exist in type 'SetStateAction'.ts(2345)
setAlertRules({ alertRules });
I think it should besetAlertRules(alertRules);
as you now provide object with field alertRules and not an array – Perilune