I have a class component which has this operation:
const notificationPreferencesEmailListIds: Set<number> = Array.isArray(
notificationPreferences
)
? new Set(
notificationPreferences.reduce(
(acc: number[], item: INotificationPreference) =>
item.isUserSignedUp ? [...acc, item.emailListId] : acc,
[]
)
)
: new Set([]);
this.state = {
notificationPreferencesEmailListIds
};
public setSubscriptionStatusById({
emailListId,
operation,
}: INotificationPreferencesStateSetter) {
this.setState((prevState) => {
prevState.notificationPreferencesEmailListIds[operation](emailListId);
return {
notificationPreferencesEmailListIds:
prevState.notificationPreferencesEmailListIds,
};
});
}
private renderPreferences() {
const { notificationPreferences } = this.props;
return (
<div className="preferences">
{
Array.isArray(notificationPreferences) &&
notificationPreferences.map((prefrence: INotificationPreference) => (
<NotificationPreference
key={prefrence.emailListId}
setParentState={this.setSubscriptionStatusById}
hideResultText={this.hideResultText}
{...prefrence}
/>
))
}
</div>
);
}
the renderPreferences() method is call child component which includes checkboxes, when we checked checkbox it call setParentState setParentState({ emailListId, operation });
operation can be "add" or "delete" according to checked state. types:
notificationPreferencesEmailListIds: Set<number>;
export interface INotificationPreferencesStateSetter {
emailListId: number;
operation: "add" | "delete";
}
When I wan't to convert it useState like this:
const setSubscriptionStatusById = ({ emailListId, operation }: INotificationPreferencesStateSetter) => {
setNotificationPreferencesEmailListIds((prevState: any) => {
prevState.notificationPreferencesEmailListIds[operation](emailListId);
return prevState.notificationPreferencesEmailListIds;
});
};
it gives me error "add" or "delete" is undefined when call the child component.
How can I convert this setState to useState ?
Via Active questions tagged javascript - Stack Overflow https://ift.tt/2FdjaAW
Comments
Post a Comment