【问题标题】:simple server/client routing react-express(nodejs)简单的服务器/客户端路由 react-express(nodejs)
【发布时间】:2018-02-25 00:27:47
【问题描述】:

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>
        `);
});

【问题讨论】:

    标签: node.js reactjs render-to-string react-dom-server


    【解决方案1】:

    在您的 React 组件中,只需触发您需要的 API 调用

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

    通常使用 SPA,您会在第一个请求时从服务器提供完整的客户端应用程序,为您的视图使用客户端路由,然后触发任何 API 请求(如示例)。

    【讨论】:

      【解决方案2】:

      更新: 我认为您可能会发现本教程对您有所帮助: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. 在 App JSX 周围加上 &lt;Router /&gt;。
      2. 使用&lt;Link /&gt; 创建指向&lt;Route /&gt; 的链接。当路径 在链接匹配中,正确的路线呈现。
      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;
      

      【讨论】:

      • @stackoverflow.com/users/2885592/greg-artemides - 这是用于服务器端渲染...对不起,如果我的问题不清楚。
      • 只是为了确保我理解正确 - 你有一个基于 Express 构建的 api,你的客户端应用程序是 React,你想在服务之前在服务器上渲染 React?
      • 是的,我有 api 端点,我应该从 Node(Express) 调用它并在服务器上呈现响应并填充反应组件。
      • 我的主要问题是我有 2 个路由 - 2 个端点 - 我如何让 server.js 中的 app.get 匹配路由并进行适当的 api 调用?我读到了 rendertostring - redux 服务器端渲染我应该每个 api 调用都这样做吗?每个组件的负载?
      • 你能发布一些代码吗?查看 Express app.get(API)和 React 路由(在服务器上呈现的客户端)会很有帮助。
      【解决方案3】:

      在&lt;otherComponent \&gt;的定义中,取componentDidMount()中的数据,然后做store.dispatch({ type: 'RECEIVED_DATA', data })更新store。

      【讨论】:

        猜你喜欢
        • 2020-04-11
        • 2015-04-17
        • 2015-05-22
        • 2020-12-11
        • 2016-10-06
        • 2018-02-05
        • 2016-05-14
        • 2018-05-01
        • 1970-01-01
        相关资源
        最近更新 更多