Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用React路由器4预取数据_Javascript_React Router_React Router V4 - Fatal编程技术网

Javascript 使用React路由器4预取数据

Javascript 使用React路由器4预取数据,javascript,react-router,react-router-v4,Javascript,React Router,React Router V4,我正在尝试迁移到React路由器的v4,但我被预取数据卡住了。我们过去使用的最新版本具有功能 返回的参数之一是renderProps,但现在我不能使用match,所以我不知道如何预取数据。显示了如何加载数据,但不适用于我,因为我正在使用的新版本同步存储 以下是客户端的代码: return ( <MuiThemeProvider muiTheme={letgoMuiTheme}> <Provider store={store}> <IntlPr

我正在尝试迁移到React路由器的v4,但我被预取数据卡住了。我们过去使用的最新版本具有功能

返回的参数之一是renderProps,但现在我不能使用match,所以我不知道如何预取数据。显示了如何加载数据,但不适用于我,因为我正在使用的新版本同步存储

以下是客户端的代码:

return (
  <MuiThemeProvider muiTheme={letgoMuiTheme}>
    <Provider store={store}>
      <IntlProvider>
        { routes(history, store) }
      </IntlProvider>
  </Provider>
</MuiThemeProvider>
);

经过研究,我找到了一个适合我的解决方案。这是帮助程序(在was预取数据之前)
matchPath
来自
react路由器dom

export const getRouteData = (routesArray, url, store) => {
  const { dispatch } = store;
  const state = store.getState();
  const extractWrappedComponent = (component) => {
    if (component.WrappedComponent) {
      return extractWrappedComponent(component.WrappedComponent);
    }
    return component;
  };

  return routesArray.map((route) => {
    const component = route.component;
    const extractedComponent = extractWrappedComponent(component);
    const match = matchPath(url, { path: route.path, exact: route.exact, strict: false });

    if (match) {
      if (extractedComponent.fetchData !== undefined) {
        extractedComponent.fetchData(state, dispatch, match.params);
      }
    }
  });
}
那么我叫它:

getRouteData(routes, req.url, store);
WARE路线是:

我的服务器文件现在没有匹配项。相反:

if (context.url) {
  res.redirect(302, context.url);
} else {
  const render = () => {
    const body = renderApp(req, muiTheme, store, history, context);

    res.render('index', {
      head,
      body,
      locale,
      ...
    });
  };

  Promise.all(
    getRouteData(routes, req.url, store),
  )
  .then(render)
  .catch(prefetchError => next(prefetchError));
RenderApp文件(客户端必须是相同的,没有
StaticRouter
):

const renderApp=(请求、博物馆、存储、历史、上下文)=>{
常量应用=(
{批准(历史)}
);
返回renderToString(app);
};
批准文件:

export default function appRoutes(history) {
  return (
    <ConnectedRouter history={history}>
      <div>
        {routes.map((route, key) => (
          <Route key={key} {...route} />
        ))}
      </div>
    </ConnectedRouter>
  );
}
导出默认功能批准(历史记录){
返回(
{routes.map((route,key)=>(
))}
);
}

您找到解决方案了吗?真是愚蠢!我也有同样的问题。@DarynK。我已经用答案回答了我的问题。希望有帮助!有没有办法从主组件中包含的子组件调用fetchdata?我没有发现任何repo在做tat,使用redux saga我可以实现它。但是我没有找到任何方法来解析子组件并获取数据。
export const getRouteData = (routesArray, url, store) => {
  const { dispatch } = store;
  const state = store.getState();
  const extractWrappedComponent = (component) => {
    if (component.WrappedComponent) {
      return extractWrappedComponent(component.WrappedComponent);
    }
    return component;
  };

  return routesArray.map((route) => {
    const component = route.component;
    const extractedComponent = extractWrappedComponent(component);
    const match = matchPath(url, { path: route.path, exact: route.exact, strict: false });

    if (match) {
      if (extractedComponent.fetchData !== undefined) {
        extractedComponent.fetchData(state, dispatch, match.params);
      }
    }
  });
}
getRouteData(routes, req.url, store);
export const routes = [
  {
    path: '/',
    component: App
  },
  {
    path: '/:lang/chat',
    component: Chat
  }
  ...
if (context.url) {
  res.redirect(302, context.url);
} else {
  const render = () => {
    const body = renderApp(req, muiTheme, store, history, context);

    res.render('index', {
      head,
      body,
      locale,
      ...
    });
  };

  Promise.all(
    getRouteData(routes, req.url, store),
  )
  .then(render)
  .catch(prefetchError => next(prefetchError));
const renderApp = (req, muiTheme, store, history, context) => {
  const app = (
    <MuiThemeProvider muiTheme={muiTheme}>
      <Provider store={store}>
        <IntlProvider>
          <StaticRouter
            location={req.url}
            context={context}
          >
            { appRoutes(history) }
          </StaticRouter>
        </IntlProvider>
      </Provider>
    </MuiThemeProvider>
  );

  return renderToString(app);
};
export default function appRoutes(history) {
  return (
    <ConnectedRouter history={history}>
      <div>
        {routes.map((route, key) => (
          <Route key={key} {...route} />
        ))}
      </div>
    </ConnectedRouter>
  );
}