App.tsx的一些代码:
interface AppProps{
history: any
}
export default class App extends React.Component<AppProps,...> {
public state = {
target: this.props.history.push('/')
};
private route() {
if (!session.isLogin) {
this.setState({ target: this.props.history.push('/') });
return
} else {
this.setState({ target: this.props.history.push('/root') })
}
}
...
public render() {
return this.state.target
}
}
index.tsx的一些代码
ReactDOM.render(
<MemoryRouter>
<LocaleProvider locale={zhCn}>
<App history={ ???? }/>
</LocaleProvider>
</MemoryRouter>,
document.getElementById('root') as HTMLElement
);
registerServiceWorker();
为什么“history”的类型不对?提示:类型“{}”中缺少属性“history”。我应该在 index.tsx 中传递哪些参数?
谢谢!
最佳答案
如果你想要App
接收所有 RouteComponentProps
(包括历史对象)来自 MemoryRouter
, 那么你需要调用 App
通过一条路线,像这样:
ReactDOM.render(
<MemoryRouter>
<LocaleProvider locale={zhCn}>
<Route component={App}/>
</LocaleProvider>
</MemoryRouter>,
document.getElementById('root') as HTMLElement
);
然后你可以替换AppProps
与 RouteComponentProps<{}>
在 App
的声明中.
关于reactjs - react 路由器 : 'history' is missing in type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52531030/