gpt4 book ai didi

javascript - TS错误: Type 'unknown' is not assignable to type 'FunctionComponent due to withRouter

转载 作者:行者123 更新时间:2023-12-02 19:55:31 25 4
gpt4 key购买 nike

我收到此错误

client:159 [at-loader] ./src/App.tsx:40:46 TS2322: Type 'unknown' is not assignable to type 'FunctionComponent | ComponentClass | ComponentClass, any> | FunctionComponent> | undefined'. Type 'unknown' is not assignable to type 'FunctionComponent>'.

在此组件上:

import React, { FC, useState } from 'react';
import { Provider } from 'react-redux';
import { BrowserRouter, Route, Switch } from 'react-router-dom';
import User from './scenes/user/User';

const store = configureStore();

type IsOpen = boolean;

const App: FC = () => {
const [sidebarOpen, setSidebarOpen] = useState<IsOpen>(false);

return (
<Provider store={store}>
<BrowserRouter>
<Sidebar
open={sidebarOpen}
sidebar={<PrimaryNav closeNav={() => setSidebarOpen(false)} />}
onSetOpen={setSidebarOpen}
styles={{ sidebar: { background: 'black' } }}>
<Header sidebarOpen={sidebarOpen} onSetSidebarOpen={setSidebarOpen} />
<Switch>
<Route exact={true} path='/' component={NewsArticles} />
<Route exact={true} path='/user' component={User} />
<Route path='/user/:id' component={User} />
</Switch>
</Sidebar>
</BrowserRouter>
</Provider>
);
};

export default App;

就这两行

            <Route exact={true} path='/user' component={User} />
<Route path='/user/:id' component={User} />

这是用户组件:

import React, { FC, useEffect } from 'react';

import { withRouter } from 'react-router-dom';

type Props = {
match: any;
history: any;
location: any;
withRouter: any;
fetchUsers?: any;
users?: UserInfoModel[];
};

const mapState = (state: UsersAppState, props: Props) => {
return {
users: state.usersReducers.users,
...props
};
};

const actionCreators = {
fetchUsers
};

const User: FC<Props> = props => {
const { history, users, location, match, fetchUsers } = props;

const setID = match.params.hasOwnProperty('id') ? match.params.id : 'AbedA';

const handleFetch = () => {
return fetchUsers(setID);
};

useEffect(() => {
if (!users || !users.length) {
handleFetch();
}
}, []);

useEffect(() => {
handleFetch();
}, [location.pathname, match.params.id]);

return (
<UserContainer history={history} fetchUsers={() => fetchUsers(setID)} users={users} profilePicture={profileImg} />
);
};

export default compose(
connect(
mapState,
actionCreators
),
withRouter
)(User);

我提出的关于 react-router/react-router-dom 的每件事都带有错误。

如果我删除 withRouter 错误就会消失。

我做错了什么?

最佳答案

您可以这样解决错误:

export default compose<React.ComponentType<Props>>(
connect(
mapState,
actionCreators
),
withRouter
)(User);

关于javascript - TS错误: Type 'unknown' is not assignable to type 'FunctionComponent<any> due to withRouter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57172382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com