Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ReactJS react路由器服务器呈现问题_Javascript_Reactjs_React Router - Fatal编程技术网

Javascript ReactJS react路由器服务器呈现问题

Javascript ReactJS react路由器服务器呈现问题,javascript,reactjs,react-router,Javascript,Reactjs,React Router,一切都是关于同构应用的。我正在使用服务器端的React和React router模块进行路由,并且在浏览器控制台中出现以下警告 警告:渲染(…):使用新的 根组件。如果要更新此节点的子节点, 您应该让现有子对象更新其状态和 呈现新组件,而不是调用ReactDOM.render 我在后端上定义了以下路由模式: <Route path="/" component={App} > <IndexRoute component={Home} /> </Route>

一切都是关于同构应用的。我正在使用服务器端的
React
React router
模块进行路由,并且在浏览器控制台中出现以下警告

警告:渲染(…):使用新的 根组件。如果要更新此节点的子节点, 您应该让现有子对象更新其状态和 呈现新组件,而不是调用ReactDOM.render

我在后端上定义了以下路由模式:

<Route path="/" component={App} >
    <IndexRoute component={Home} />
</Route>
ReactDOM.render(<Home />, document.getElementById('container'));

应用程序组件:

module.exports = React.createClass({
  render : function() {
    return <html>
      <head></head>
      <body>        
        <div id="container">
          { this.props.children }
        </div>        
        <script src="/app/bundle.js"></script>
      </body>
    </html>
  }
});
module.exports = React.createClass({
  render : function() {
    return <div>Any content here</div>
  }
});
module.exports=React.createClass({
render:function(){
返回
{this.props.children}
}
});
主组件:

module.exports = React.createClass({
  render : function() {
    return <html>
      <head></head>
      <body>        
        <div id="container">
          { this.props.children }
        </div>        
        <script src="/app/bundle.js"></script>
      </body>
    </html>
  }
});
module.exports = React.createClass({
  render : function() {
    return <div>Any content here</div>
  }
});
module.exports=React.createClass({
render:function(){
在此处返回任何内容
}
});
之后,我在前端使用:

<Route path="/" component={App} >
    <IndexRoute component={Home} />
</Route>
ReactDOM.render(<Home />, document.getElementById('container'));
ReactDOM.render(,document.getElementById('container');
可能的解决方案: 如果我理解正确,如果我可以将应用程序组件呈现为静态标记(
renderToStaticMarkup
),将主组件呈现为字符串(
renderToString
),那么就可以了


有没有可能通过
react router
实现类似的功能?

假设RR 1.03,您的路由配置看起来很好

您的应用程序组件应如下所示:

module.exports = React.createClass({
  render : function() {
    return <html>
      <head></head>
      <body>        
        <div id="container">
          {React.cloneElement(this.props.children,
            {
              anyOtherPropsHere: 'blablah'
            }
          )}
        </div>        
        <script src="/app/bundle.js"></script>
      </body>
    </html>
  }
});
module.exports=React.createClass({
render:function(){
返回
{React.cloneElement(this.props.children,
{
任何其他支持者:“胡说八道”
}
)}
}
});
您的服务器响应->渲染应该如下所示。(摘自)

从'react dom/server'导入{renderToString}
从“react router”导入{match,RouterContext}
从“/routes”导入路由
服务((请求、回复)=>{
//请注意,这里的req.url应该是来自的完整url路径
//原始请求,包括查询字符串。
匹配({routes,location:req.url},(错误,重定向位置,renderProps)=>{
如果(错误){
资源状态(500).发送(错误消息)
}else if(重定向位置){
res.redirect(302,redirectLocation.pathname+redirectLocation.search)
}else if(渲染器操作){
//您还可以检查RenderOps.components或RenderOps.routes中的
//您的“未找到”组件或路由,并发送404作为
//下面,如果您使用的是全面覆盖路线。
res.status(200).send(renderToString())
}否则{
res.status(404.send)(“未找到”)
}
})
})
最后,在客户端负载的某个地方,执行类似的操作。我在本例中添加了库以提供帮助

import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, match, RoutingContext } from 'react-router';
import history from 'utils/history';
import AppRoutes from '/app/AppRoutes';

// use this function to return a Route component with the right props
function createFluxComponent(Component, props) {
    props = Object.assign(props, {
        flux: window.flux.or.whatevs
    });

    return <Component {...props} />;
}

// add a global history listener perhaps?
history.listen(function(location) {
    console.log('Transition to--------------', location.pathname);
});

// add window.router polyfill for transitionTo
window.router = {
    transitionTo: function(t) {
        return history.pushState(null, t);
    }
};

// render your routing configuration, history and flux as  props
ReactDOM.render(<Router createElement={createFluxComponent} history={history} routes={AppRoutes}/>, document);
从“React”导入React;
从“react dom”导入react dom;
从“react Router”导入{Router,Route,match,RoutingContext};
从“utils/history”导入历史;
从“/app/apprutes”导入批准;
//使用此函数返回带有正确道具的路线组件
函数createFluxComponent(组件、道具){
道具=对象。分配(道具{
通量:window.flux.or.whatevs
});
返回;
}
//是否添加一个全局历史侦听器?
历史。监听(功能(位置){
console.log('转换到-------------------',location.pathname);
});
//添加window.router polyfill以过渡到
window.router={
过渡到:函数(t){
返回history.pushState(null,t);
}
};
//将路由配置、历史和流量渲染为道具
ReactDOM.render(,文档);

最重要的是在服务器端使用RouterContext和渲染道具渲染到字符串。您可以从RR match函数获得renderProps,该函数将根据配置运行路由,并在renderProps中生成正确的组件。然后,您只需使用Router元素和Router配置将客户端呈现到文档中。这有意义吗?它应该在没有任何不变量的情况下工作。

假设RR 1.03,您的路由配置看起来很好

您的应用程序组件应如下所示:

module.exports = React.createClass({
  render : function() {
    return <html>
      <head></head>
      <body>        
        <div id="container">
          {React.cloneElement(this.props.children,
            {
              anyOtherPropsHere: 'blablah'
            }
          )}
        </div>        
        <script src="/app/bundle.js"></script>
      </body>
    </html>
  }
});
module.exports=React.createClass({
render:function(){
返回
{React.cloneElement(this.props.children,
{
任何其他支持者:“胡说八道”
}
)}
}
});
您的服务器响应->渲染应该如下所示。(摘自)

从'react dom/server'导入{renderToString}
从“react router”导入{match,RouterContext}
从“/routes”导入路由
服务((请求、回复)=>{
//请注意,这里的req.url应该是来自的完整url路径
//原始请求,包括查询字符串。
匹配({routes,location:req.url},(错误,重定向位置,renderProps)=>{
如果(错误){
资源状态(500).发送(错误消息)
}else if(重定向位置){
res.redirect(302,redirectLocation.pathname+redirectLocation.search)
}else if(渲染器操作){
//您还可以检查RenderOps.components或RenderOps.routes中的
//您的“未找到”组件或路由,并发送404作为
//下面,如果您使用的是全面覆盖路线。
res.status(200).send(renderToString())
}否则{
res.status(404.send)(“未找到”)
}
})
})
最后,在客户端负载的某个地方,执行类似的操作。我在本例中添加了库以提供帮助

import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, match, RoutingContext } from 'react-router';
import history from 'utils/history';
import AppRoutes from '/app/AppRoutes';

// use this function to return a Route component with the right props
function createFluxComponent(Component, props) {
    props = Object.assign(props, {
        flux: window.flux.or.whatevs
    });

    return <Component {...props} />;
}

// add a global history listener perhaps?
history.listen(function(location) {
    console.log('Transition to--------------', location.pathname);
});

// add window.router polyfill for transitionTo
window.router = {
    transitionTo: function(t) {
        return history.pushState(null, t);
    }
};

// render your routing configuration, history and flux as  props
ReactDOM.render(<Router createElement={createFluxComponent} history={history} routes={AppRoutes}/>, document);
从“React”导入React;
从“react dom”导入react dom;
从“react Router”导入{Router,Route,match,RoutingContext};
从“utils/history”导入历史;
从“/app/apprutes”导入批准;
//使用此函数返回带有正确道具的路线组件
功能铬