Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-router: 'history' is missing in type

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!

like image 533
cc shen Avatar asked Nov 07 '22 01:11

cc shen


1 Answers

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.

like image 192
Matt McCutchen Avatar answered Nov 14 '22 23:11

Matt McCutchen