some code of 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
}
}
some code of index.tsx
ReactDOM.render(
<MemoryRouter>
<LocaleProvider locale={zhCn}>
<App history={ ???? }/>
</LocaleProvider>
</MemoryRouter>,
document.getElementById('root') as HTMLElement
);
registerServiceWorker();
Why is the type of "history" wrong? tips:Property 'history' is missing in type '{}'. What parameters should I pass in index.tsx?
Thank you!
If you want App
to receive all of the RouteComponentProps
(including the history object) from the MemoryRouter
, then you need to call App
via a route, something like this:
ReactDOM.render(
<MemoryRouter>
<LocaleProvider locale={zhCn}>
<Route component={App}/>
</LocaleProvider>
</MemoryRouter>,
document.getElementById('root') as HTMLElement
);
Then you can replace AppProps
with RouteComponentProps<{}>
in the declaration of App
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With