Skip to content

关于dva1.x升级到2.x #1

@programmer-yang

Description

@programmer-yang

dva 1.x => 2.x

记录自己在升级dva到2.x中遇到的问题,如果发现写的不对的地方请不要笑,及时沟通,新手司机上路 ^_^

参考资料

问题一:location.query

由于history版本变动,location不在包含有query对象,需要手动处理

yarn add query-string
import queryString from 'query-string';
history.listen((location) => {
const query = queryString.parse(location.search);
// console.log(query);
});

问题二 嵌套路由

R4里修改了对于嵌套路由的实现方式

// old
<Router history={history}>
  <Route path="/home" component={Container}>
    <Route path="/home/page1" component={Page1} />
    <Route path="/home/page2" component={Page2} />
  </Route>
</Router>

// new
<Router history={history}>
  <Route path="/home">
    <Container>
      <Route path="/home/page1" component={Page1} />
      <Route path="/home/page2" component={Page2} />
    </Container>
  </Route>
</Router>

// or
<Router history={history}>
  <Route path="/home" render={(props) => {
    return (
      <Container {...props}>
        <Route path="/home/page1" component={Page1} />
        <Route path="/home/page2" component={Page2} />
      </Container>
    );
  }}>
  </Route>
</Router>

最后一种写法主要用于Container组件中需要用到history location match 的情况

R4取消了关于on****钩子函数,相关实现可以在Container容器组件的生命周期里来做

关于Redirect

R4 删除了IndexRedirect,需要重定向的时候使用Redirect
Redirectto属性现在和Route的path属性一样,适配所有能适配上的路径

// error
<Switch>
  <Redirect from="/home" to="/home/page1" />
  <Route path="/home/page1" component={Page1} />
  <Route path="/home/page2" component={Page2} />
</Switch>

这样的写法会报错,因为form的值会适配到/hom/page1 /home/page2这样的所有子路径,就会出现理论上的递归跳转(会报错,不会递归,只是形容一下)
Redirect加上exact属性就好了

<Switch>
  <Redirect exact from="/home" to="/home/page1" />
  <Route path="/home/page1" component={Page1} />
  <Route path="/home/page2" component={Page2} />
</Switch>

关于Link
R4的Link拆分成了两个:Link NavLink
简单的跳转使用Link
特殊的跳转(有选中样式需求:activeClassName)使用NavLink
参考

有使用到QueueAnimLink添加动画的地方要修改为NavLink

问题三 dispatch effect 回调

在dva2.x中新增了effect返回promise的特性,现在写回调逻辑越来yue方便了

dispatch({ type: 'home/xxx' })
  .then(({ data = {} }) => {
    console.log(data);
  })
  .catch((e) => {
    // error
    console.log(e);
  });

问题四 关于使用browserHistory

在之前的版本中,我们去掉path中的#的方式是

import {browserHistory} from 'dva/router';
const app = dva({
history: browserHistory,
});

在2.x版本中需要修改为:

yarn add history
import createHistory from 'history/createBrowserHistory';
const history = createHistory();
const app = dva({
  history,
});

这里到将来可能还会有一些修改,但目前这么使用是可行的

其他相关问题

关于webpack插件html-webpack-plugin

背景:

dva之前的版本不支持文件hash,现在dva已经支持了这个功能 链接
使用方式是在.roadhog中添加hash:true的配置,默认约定在src下有一个index.ejs的模板

问题

Uncaught TypeError: Cannot read property '__reactInternalInstance$ufa3tpnn4i' of null
    at Object.getClosestInstanceFromNode (8:113)
    at findParent (
<script src="./index.js"></script>
<script type="text/javascript" src="/index.js?a6d74e09fa325aa523a2"></script>

解决方式

问题就是引入了两次index.js
方法一:删除模板中的index.js的引用
方法二:

module.exports = (webpackConfig) => {
  const htmlWebpackPlugin = new HtmlWebpackPlugin({
    filename: 'index.html',
    template: `!!html!./src/${process.env.LOCAL_DIR}/index.html`,
    hash: true,
    inject: false,
    minify: {
      collapseBooleanAttributes: true,
    },
  });
  webpackConfig.plugins.push(htmlWebpackPlugin);
  return webpackConfig;
};

webpack.config.js的关于html-webpack-plugin的配置内容里添加inject: false
参考

inject: false,会使注入失效,如果需要hash请使用方法一

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions