在服务器端使用 basename 的动态段 - React-Router

IT技术 reactjs react-router isomorphic-javascript
2022-07-11 00:34:19

我将以下代码写入我server.js的同构渲染。

import { match, useRouterHistory } from 'react-router';
import createHistory from 'react-router/lib/createMemoryHistory';

import createHistory from 'react-router/lib/createMemoryHistory';
import { useBasename } from 'history'; 
import getRoutes from './routes';


const client = new ApiClient(req);
const memoryHistory = useRouterHistory(createHistory)({
    basename: '/in'
  });
const store = createStore(memoryHistory, client);
const history = syncHistoryWithStore(memoryHistory, store);

match({ history, routes: getRoutes(store), location: req.originalUrl }, (error, redirectLocation,   renderProps) => {
    console.log(error, redirectLocation, renderProps)
    // undefined, undefined, undefined returned
})

现在,当我稍微调整参数以检查问题可能出在哪里时,我进行了以下更新。

match({ history, routes: getRoutes(store) }, (error, redirectLocation, renderProps)

我最终得到以下日志:

null { pathname: '/in/leagues',
  search: '',
  hash: '',
  state: null,
  action: 'REPLACE',
  key: 'meh7oq',
  basename: '/in',
  query: {} } undefined

是否缺少某些参数,或者此基本名称是否不适用于服务器端渲染。

我也尝试过使用useBasename历史中的功能

const memoryHistory = useBasename(createHistory)({
    basename: '/in'
});
0个回答
没有发现任何回复~