反应服务器不启动 – 与renderProps的东西

我有一个React应用程序,这里是我的服务器代码:

app.get('*', (req, res) => { let history = createMemoryHistory(); let store = configureStore(); let routes = createRoutes(history); match({ routes, location: req.url }, (error, redirectLocation, renderProps) => { if (redirectLocation) { res.redirect(301, redirectLocation.pathname + redirectLocation.search); } else if (error) { res.status(500).send(error.message); } else if (renderProps == null) { res.status(404).send('Not found'); } else { let { query, params } = renderProps; let comp = renderProps.components[renderProps.components.length - 1]; console.log('fetching'); comp.fetchData({ store, params }) .then(() => { console.log('done fetching'); let html = ReactDOMServer.renderToString( <Provider store={store}> { <RouterContext {...renderProps}/> } </Provider> ); const template = store.getState().template; const og = templateToOpenGraph(template); const full = wrap(html, store.getState(), og); res.set({ 'Cache-Control': 'public, max-age=300' }) res.send(full); }) } }); }) 

当我启动服务器时,它启动就好了。 但是,当我打一条路线(任何路线),我得到一个错误: TypeError: comp.fetchData is not a function

我需要做什么? 我不是最好的反应,所以如果我错过了明显的东西,请让我知道。

谢谢!

组件中应该有方法fetchData

我没有你的路线configuration,但是例如你在你的routes.js文件中有这样的东西

<Route path="/" component={Root}> <Route path="view/:id" component={View} /> ... </Route>

当您在浏览器中加载页面http://example.com/view/1231时,组件View将被渲染。 你应该实现方法fetchData 。 这个方法将在渲染View之前调用

 class View extends React.Component { fetchData() { // implement fetch data here } ... }