Navigating Programmatically in React-Router v4

后端 未结 10 1126
后悔当初
后悔当初 2020-12-02 11:16

I couldn\'t wait and I jumped into using the latest alpha version of react-router v4. The all-new is great in keeping your U

10条回答
  •  抹茶落季
    2020-12-02 12:04

    I don't have enough reputation to comment, but in answer to @singularity's question, you have to include the context properties you wish to make available on the component class' contextTypes static property.

    From the React docs on context:

    If contextTypes is not defined, then context will be an empty object.

    In this case:

    class NavigateNext extends React.Component {
    
      // ...
    
      static contextTypes = {
        router: PropTypes.object
      }
    
      // ...
    
    }
    

    Unlike propTypes, contextTypes actually cause React to behave differently and is not only for typechecking.

提交回复
热议问题