反应路由器5.0.1中反应路由器2.8的等效匹配功能|反应|反应路由器

时间:2019-08-14 09:10:39

标签: javascript reactjs react-router-v4

我有一个用React 15.6编写的应用程序。我想将其迁移到最新版本的React 16.9。在此应用程序中,存在一个SSR概念以使用express呈现应用程序。将当前位置与现有路线匹配的代码如下:

match({ history, routes }, (error, redirectLocation, renderProps: any) => {
      if (error) {
        res.status(500).send(error.message);
      }
      else if (redirectLocation) {
        res.redirect(redirectLocation.pathname + redirectLocation.search);
      }
      else if (renderProps) {
        let component = renderProps.components[renderProps.components.length - 1];

        if (component.hasOwnProperty('sub')) {
          component = (component as any).sub;
        }
        else if (component.hasOwnProperty('main')) {
          component = (component as any).main;
        }

        (global as any).baseURL = `https://${host}`;

        let promise = Promise.resolve();
        if (component.hasOwnProperty('getInitialData')) {
          promise = promise.then(() => (component as any).getInitialData(store.dispatch, renderProps.params, cookies));
        }

        promise
          .then(() => {
            const html = renderToString(
              <Provider store={store}>
                <div className="container">
                  <RouterContext {...renderProps} />
                  <Spinner />
                  <CSRF />
                </div>
              </Provider>
            );

            const title = DocumentTitle.rewind();

            res.render('index', {
              html,
              title,
              theme,
              config,
              state: JSON.stringify(store.getState()),
              script: scriptServiceWorker,
              timeRendered,
              hostTrail
            });
          })
        .catch((e) => {
          console.error(e);
          res.send(e);
        });
      }
    });

我想将以上代码转换为最新的react版本,但匹配项不是函数。那么有人可以帮助我如何更改上面的代码吗?哪些新的组件和软件包功能可以为我提供帮助。并且告诉我有没有可用的工具可以将整个代码迁移到最新版本。

0 个答案:

没有答案