i am using ionic-react with below code and its giving error
Type '{ state: IState; dispatch: React.Dispatch; }' is not assignable to type 'IState'. Object literal may only specify known properties, and 'state' does not exist in type 'IState'.
The code looks like below
State.tsx
import React from "react";
export interface IState {
count: number;
loggedIn: boolean;
}
// set the initial values
const initialState = { count: 0, loggedIn: false };
export type ActionType =
| { type: "setLoggedIn"; payload: any }
| { type: "error" };
// create the context
export const Context = React.createContext<IState>(initialState);
export const TheProvider = ({ children }: any): any => {
/**
* @param {*} state
* @param {*} action
*/
const reducer = (state: IState, action: ActionType): IState => {
switch (action.type) {
case "setLoggedIn":
return { ...state, ...action.payload };
default:
throw new Error();
}
};
const [state, dispatch] = React.useReducer(reducer, initialState);
// wrap the application in the provider with the initialized context
return (
<Context.Provider value={{ state, dispatch }}>{children}</Context.Provider>
);
};
export default Context;
Login.tsx
import AppContext, { TheProvider, IState } from './State';
...
const Login: React.FC = () => {
const { state, dispatch } = React.useContext<any>(AppContext);
const doLogin = async () => {
try{
dispatch({
type: 'setLoggedIn',
payload: {loggedIn: false}
})
}catch(err){
console.error("failed to login with erro", err)
}
};
return (
<TheProvider>
<form className="ion-padding">
<IonToolbar>
<IonTitle>Login</IonTitle>
</IonToolbar>
<IonItem style={{paddingTop:'100px'}}>
<IonLabel position="floating">Email</IonLabel>
<IonInput type="email" value={email} onIonChange={e => setEmail(e.detail.value!)}/>
</IonItem>
<IonItem>
<IonLabel position="floating">Password</IonLabel>
<IonInput type="password" value={password} onIonChange={e => setPassword(e.detail.value!)}/>
</IonItem>
<IonItem>
<IonLabel>{errMessage}</IonLabel>
</IonItem>
<IonButton className="ion-margin-top" onClick={doLogin} expand="block">
<IonIcon slot="start" icon={mailOutline} />Login
</IonButton>
</form>
</TheProvider>
)
};
i am now getting error on dispatch as
TypeError: dispatch is not a function at doLogin