从这里:
"在React之外获取React Component实例句柄的唯一方法是存储React.render的返回值."
我需要在React之外渲染一个React组件,我将在下面提到它的原因.
在我的node.js,expressJS应用程序中,我使用'react-router-component'和'react-async'.
在app.js中 - 应该运行的文件,
var url=require('url');
var App=require('./react/App.jsx');
var app = express();
app.get('*',function(req,res){
//}); SEE EDIT 1 BELOW
var path = url.parse(req.url).pathname;
ReactAsync.renderComponentToStringWithAsyncState(App({path:path}),function(err, markup) {
res.send('<!DOCTYPE html>'+markup);
});
});
Run Code Online (Sandbox Code Playgroud)
在App.jsx中,
PostList = require('./components/PostList.jsx');
var App = React.createClass({
render: function() {
return (
<html>
<head lang="en">
</head>
<body>
<div id="main">
<Locations path={this.props.path}>
<Location path="/" handler={PostList} />
<Location path="/admin" handler={Admin} />
</Locations>
<script type="text/javascript" src="/scripts/react/bundle.js"></script>
<script type="text/javascript" src="/scripts/custom.js"></script>
</body>
</html>
});
Run Code Online (Sandbox Code Playgroud)
bundle.js …