我正在运行graphql突变:
interface SignInReponse {
loginEmail : {
accessToken: number;
}
}
const [login] = useMutation<SignInReponse>(LOGIN);
let submitForm = (email: string, password: string) => {
setIsSubmitted(true);
login({
variables: {
email: email,
password: password,
},
})
.then(({ data }: ExecutionResult<SignInReponse>) => {
if (data !== null && data !== undefined){
dispatch({ type: "login", payload: data.loginEmail.accessToken })
setTimeout(_ => {
setShouldRedirect(true);
}, 2000)
setIsLoggedIn(true);
}})
我的登录还原器:
const initialState = {
token: null
};
export const loginReducer = (state = initialState, { type, payload } :any) => {
switch (type) {
case "login":
return {...state, token: payload};
default:
return state;
}
};
我想更改any
数据类型{ type, payload } :any
并更具体。
但是,我不知道查找/使用正确数据类型的正确方法是什么。我已经尝试过使用SignInResponse了,但是由于类型/有效载荷未提及,所以它不起作用。
编辑:
我也尝试过这个:
export const loginReducer = (state = initialState, { type, payload }: {type: "login", payload: string })
但它似乎不起作用。
TypeScript error in /helpers/privateRouting.tsx(7,32):
Property 'token' does not exist on type 'never'. TS2339
我修改了这样的内容,但仍然出现错误:
动作/登录名:
const LOGIN = 'LOGIN'
export interface LoginAction {
type: typeof LOGIN
payload: string;
}
allActions.ts:
import { LoginAction } from './login';
export type AnyAction = LoginAction
Login Reducer:
const initialState = {
token: null
};
export const loginReducer = (state = initialState, action: AnyAction) => {
switch (action.type) {
case 'LOGIN':
return {...state, token: action.payload};
default:
return state;
}
};
减速器index.ts:
export const rootReducer = combineReducers({
login: loginReducer,
});
专用路由:
export const PrivateRoute = ({ component, ...rest }: any) => {
const routeComponent = (props: any) => {
if (store.getState().login.token) {
return React.createElement(component, props);
} else {
return <Redirect to={{ pathname: '/404' }} />;
}
};
return <Route {...rest} render={routeComponent} />;
};
错误:
Property 'token' does not exist on type 'never'. TS2339
5 | export const PrivateRoute = ({ component, ...rest }: any) => {
6 | const routeComponent = (props: any) => {
> 7 | if (store.getState().login.token) {
商店index.ts:
const persistConfig = {
key: 'root',
storage
}
const persistedReducer = persistReducer(persistConfig, rootReducer)
let store = createStore(persistedReducer);
let persistor = persistStore(store);
export { store, persistor };
我正在使用如下的privateRoutes:
export default function App() {
return (
<Provider store={store}>
<PersistGate persistor={persistor}>
<div>
<BrowserRouter>
<Switch>
<Route path="/login" component={LoginPage}></Route>
<Route path="/404" component={Error404Page}></Route>
<PrivateRoute
path="/add"
component={AddUserPage}
/>
<Redirect from="*" to="/404" />
</Switch>
</BrowserRouter>
</div>
</PersistGate>
</Provider>
);
}
将Redux与TypeScript一起使用时,这是一个常见的主题。您应该actions.ts
在重要位置使用从某个大主文件导出的联合类型。例如:
// somewhere/ComponentOne.tsx
export default ComponentOne = () => stuff;
export interface OneAction {
type: 'ONE_ACTION';
payload: string;
}
// all-actions.ts
import { OneAction } from './somewhere/ComponentOne';
import { AnotherAction } from './somewhere/AnotherComponent';
export default type AnyAction = OneAction | AnotherAction;
然后在您的减速器中:
import { AnyAction } from '../../all-actions.ts';
function loginReducer(state = initialState, action: AnyAction) {
// stuff
}
现在,您的异径管完全知道它所得到的,并且类型安全性是完美的。本文比我能更好地解释它,非常值得一读。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句