0

App.js 有一个链接 - 我不明白我是否需要动作创建者/减少者?如果是这样,如何将它与服务器端渲染连接起来?

基本上加载 otherComponent - 应该通过路径从 server.js 进行 API 调用并将响应注入回组件 - 反应动作在这张图片中的位置是什么?

server.js:

app.get('/test', (req, res) => {
    res.send(<otherComponent />); -> this is returning json right now? How to handle this path for example here - where this should make an api call.
});

app.get('*', (req, res) => {
  res.send(`
            <!doctype html>
            <html>
                <head>
                    <title>My website</title>
                    <meta name="viewport" content="width=device-width, initial-scale=1">
            </head>
                <body>
                    <div id='app'>${renderToString(
                        <Provider store={createStore(reducers)}>
                            <StaticRouter location={req.url} context={{}}>
                                <App />
                            </StaticRouter>
                        </Provider>
                    )}</div>
                    <script src='bundle.js'></script>

                </body>
            </html>
        `);
});
4

3 回答 3

0

在 的定义中<otherComponent \>,获取 中的数据componentDidMount(),然后store.dispatch({ type: 'RECEIVED_DATA', data })更新存储。

于 2017-09-16T10:16:50.993 回答
0

更新: 我认为您可能会发现本教程有帮助:https ://medium.com/@foxhound87/server-side-rendering-with-react-router-we-must-react-ep-04-ad03b6b9e05d


使用create-react-app创建您的应用程序。yarn add react-router-dom添加 React 的路由器。更多信息在这里:https ://reacttraining.com/react-router/web/guides/quick-start 。

  1. 放置<Router />App JSX。
  2. 使用<Link />创建指向 的链接<Route />。当 Link 中的路径匹配时,将呈现正确的 Route。
  3. 还要注意componentDidMount.

import React, { Component } from 'react';
import { BrowserRouter as Router, Route, Link } from 'react-router-dom';
import logo from './logo.svg';
import './App.css';

class App extends Component {
  render() {
    return (
      <Router>
        <div className="App">
          <div className="App-header">
            <img src={logo} className="App-logo" alt="logo" />
            <h2>Welcome to React</h2>
          </div>
          <nav>
            <Link to="/">Home</Link>
            <Link to="/test">Test</Link>
          </nav>
          <Route path="/" exact component={Index} />
          <Route path="/test" exact component={Test} />
        </div>
      </Router>
    );
  }
}

class Index extends Component {
  onComponentDidMount() {
    fetch('http://yourapi.com')
      .then(data => this.setState(data));
  }
  render() {
    return (
      <div>
        <h1>Index Component</h1>
        <div>{ /* do something with this.state.data */}</div>
      </div>
    );
  }
}

class Test extends Component {
  onComponentDidMount() {
    fetch('http://yourapi.com')
      .then(data => this.setState(data));
  }
  render() {
    return (
      <div>
        <h1>Test Component</h1>
        <div>{ /* do something with this.state.data */}</div>
      </div>
    );
  }
}

export default App;
于 2017-09-15T19:52:26.387 回答
0

In your React component, just fire the API calls you need

componentDidMount() {
    $.get('/test', data => {
        // use data
    });
} 

Typically with an SPA, you'd serve the full client app from the server on the first request, use client-side routing for your views and then fire any API requests (like the example).

于 2017-09-15T19:09:12.503 回答