新特性

  • 完全兼容老版本v4
  • 支持 React 16 , 兼容 React >= 15
  • 消除在 React.StrictMode 严格模式中的警告
  • 使用 create-react-context 实现 context API

    新功能

  • Route组件path可以为数组

    • before

      1. <Switch>
      2. <Route path="/users/:id" component={User} />
      3. <Route path="/profile/:id" component={User} />
      4. </Switch>
    • after

      1. <Route path={["/users/:id", "/profile/:id"]} component={User} />
  • 支持React.createRefin<Link innerRef>

    • 举个栗子
      1. // 通过以下方式可以获取到 Link 组件对应的 DOM 元素
      2. this.createRef = React.createRef();
      3. <Link to="/home" innerRef={this.createRef}>home</Link>
  • 支持React.forwardRefin<Route component>

    • 举个栗子
      1. <Route path="/home" component={Home}/>
      2. // Home 组件
      3. export default React.forwardRef((props, ref) => {
      4. // 纯函数组件也能获取到 ref (当前 Home 组件对应的 DOM 元素)
      5. console.log(props, ref);
      6. return <h2>Home</h2>
      7. })