【问题标题】:ReactJS server side rendering and componentDidMount methodReactJS 服务端渲染和 componentDidMount 方法
【发布时间】:2017-05-16 05:16:54
【问题描述】:

我是 React 新手,所以请不要严格判断。 我正在服务器上渲染我的 React 应用程序,并希望在前端执行代码。 应用程序使用样式正确呈现并且没有警告或错误,尽管状态为空,因为我使用的是应该在前端执行的 API,现在可以了。

据我了解,服务器渲染组件,并且由于服务器在服务器上渲染和安装组件,它没有调用 componentDidMount() 方法 这应该做我的 API 调用和其他工作人员

这是我的组件

import React from 'react';
import {render} from 'react-dom';
import SpComparison from './spComparison.jsx';
import Comparison from './comparison.jsx';
import AnalystRatings from './analystRatings.jsx';


export default class Insights extends React.Component {
constructor(props){
    super(props);
    this.state = {
       charts: []
    }

    let _this = this;
}

componentDidMount() {
    console.log("I want to do log on front end side, but can't")
}

render () {
    let charts = this.state.charts.map(function(i){
        if (i.type == 'comparison'){
            return <Comparison key={ i.id } config={ i.config } />
        }
        else if (i.type == 'sp-comparison'){
            return <SpComparison key={ i.id } config={ i.config } />
        }
        if (i.type == 'analyst-ratings'){
            return <AnalystRatings key={ i.id } config={ i.config } />
        }
    });

    return (
        <div id="insights" className="container">
            <div className="row">
                <div className="col-md-3 hidden-md-down" style={{
                    marginTop: 10
                }}>
                    <ul className='finstead-tabs'>
                        <li className="finstead-tabs-header">
                            Categories
                        </li>
                        <li>
                            <a href='' className="finstead-active-tab">Performance</a>
                        </li>
                        <li>
                            <a href=''>Financial</a>
                        </li>
                        <li>
                            <a href=''>Valuation</a>
                        </li>
                        <li>
                            <a href=''>Shares</a>
                        </li>
                        <li>
                            <a href=''>Outlook</a>
                        </li>
                    </ul>
                </div>
                <div className="col-xs-12 col-md-9">
                    { charts }
                </div>
            </div>
        </div>
    )
 }
}

我想我的做法不对,所以请帮助我。

注意

在最高级别的组件中我没有调用 ReactDOM.render 可能会导致这种行为吗?

tutorial 我用的例子

【问题讨论】:

    标签: javascript reactjs rendering react-router single-page-application


    【解决方案1】:

    我在仔细阅读tutorial之后找到了解决方案。

    问题是我的疏忽,一切都在上面的教程中描述。

    基本上在捆绑文件中,您应该调用ReactDOM.render 再次执行应用程序,但这不会影响性能,因为 React 使用 VirtualDOM 并且与现有的 DOM 存在差异。

    所以没有ReactDOM.render js 将不会被执行。

    所以我创建了单独的文件 app-script.js,这是我的 bundle 入口点,它使用 ReactDOM.render 调用我的最高组件。

    【讨论】:

    • 在您的情况下,调用 ReactDOM.render 将是一件大事,因为您的服务器端呈现的 HTML 是基于无状态的。所有基于您在客户端构建的状态的额外 HTML 都需要区分为 DOM,这将花费额外的时间。我不明白你为什么在这里做服务器端渲染,你的速度提升是微不足道的。
    • 如前所述,我还在学习中,完全理解没有数据的服务器端渲染是无用的,所以一切都有它的时间,现在学习Redux,希望能为服务器端渲染创建状态,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2023-04-09
    • 2015-05-28
    • 1970-01-01
    • 2015-03-01
    • 1970-01-01
    • 2017-09-09
    • 1970-01-01
    相关资源
    最近更新 更多