Reactjs 反应路由器服务器端匹配转换的路由?

Reactjs 反应路由器服务器端匹配转换的路由?,reactjs,internationalization,react-router,locale,universal,Reactjs,Internationalization,React Router,Locale,Universal,我正在使用react router进行服务器端渲染,并将区域设置信息存储在locales.json文件中。仅在api调用响应后设置区域设置信息,该api调用包括当前语言,即“”GB“、”NO“、”FR”等“”,然后完成服务器响应,并以正确的语言将所有内容发送到客户端 但是,我使用的是react routermatch方法: match({ routes, location: req.url }, (error, redirectLocation, renderProps) => { ...

我正在使用react router进行服务器端渲染,并将区域设置信息存储在
locales.json
文件中。仅在api调用响应后设置区域设置信息,该api调用包括当前语言,即“
”GB“、”NO“、”FR”等“
”,然后完成服务器响应,并以正确的语言将所有内容发送到客户端

但是,我使用的是
react router
match
方法:

match({ routes, location: req.url }, (error, redirectLocation, renderProps) => { ... }
…我需要
路由
基于api响应的语言,即

// Route
<Route path={`:storeId/${locales[language].path}`} />

// locale.json
{
  "GB": {
    "path": "contact"
  },
  "NO": {
    "path": "kontakt"
  }
}
//路由
//locale.json
{
“GB”:{
“路径”:“联系人”
},
“否”:{
“路径”:“kontakt”
}
}

这种方法可行吗?就像我需要在api调用后定义路由一样,但要进行api调用,我需要定义路由。

是的,我没有具体尝试过您的示例,但从api响应定义路由并将其传递给“匹配”函数是完全可能的

您可以尝试以下方法:

function handleServerRendering(req, res) {

  axios.get('http://localhost:3001/myCutomeRoutes')
   .then(function(response){

     const myRoutes = {
       routes: response.data,
       location: req.url
     }

     match(myRoutes, function(error, redirectLocation, routeContext) {
      // WRITE YOUR CODE IN HERE...
     })
   })
   .catch(function(err){
     console.log('error', err);
   })
}
如您所见,首先进行API调用,然后将response.data传递给“myRoutes”常量中的routes