我创建一个反应和nodejs isomprphic店。 我的问题是如何用SERVER SIDE上的初始数据填充我的组件。 我知道如何使用初始数据填充我的主页组件,我在“/”路径中获取数据并将它们作为道具传递给我的组件。但其他路线呢? 我可以定义多个服务器端路由,但我的网站不再是SPA。 如何才能得到反应路由器客户端路由和服务器端启动数据的好处?反应同构和初始数据
1
A
回答
2
REDX可以节省您一些时间做这样的事情。或者你可以在服务器上计算状态,并将其填充为状态存储应用程序的道具。
React路由器很容易同构,但你必须通过你的状态。
这index.jsx是我的榜样与反应路由器W/O终极版:
"use strict";
import React from 'react';
import { render } from 'react-dom';
import { match, Router, browserHistory } from 'react-router';
const injectTapEventPlugin = require('react-tap-event-plugin');
injectTapEventPlugin();
//for dev tools
window.React = React;
const routes = require('./Routes.jsx');
const history = browserHistory;
match({history, routes}, (error, redirectLocation, renderProps) => {
render(<Router {...renderProps} />, document.getElementById('app'));
})
和Routes.jsx你会发现这样的:
"use strict";
const React = require('react');
import { Router, Route, Link, IndexRoute } from 'react-router'
const Layout = require("./components/Layout.jsx");
const Login = require("./components/Login.jsx");
const Home = require("./components/Home.jsx");
const NotFound = require("./components/NotFound.jsx");
const routes = (
<Route path="/" component={Layout}>
<IndexRoute component={Home}/>
<Route path="login" component={Login}/>
<Route path="*" component={NotFound}/>
</Route>
);
module.exports = routes;
0
我用服务器端的以下组件,用于具有react-router的SPA,传递位置prop以及ReactDOMServer.renderToString()中的初始状态对象()
裁判:React-Router Server Rendering docs
import React, { Component } from 'react';
import { createStore, combineReducers } from 'redux';
import { Provider } from 'react-redux';
import reducers from '../reducers';
import routes from '../routes';
import { match, RoutingContext } from 'react-router';
export default class App extends Component {
constructor(props) {
super(props);
}
render() {
let reducer = combineReducers(reducers);
let store = createStore(reducer, this.props);
var component;
match({ routes, location: this.props.location }, function (error, redirectLocation, renderProps) {
if (error) {
component = <p>{error.message}</p>;
} else if (renderProps) {
component = <RoutingContext {...renderProps} />;
} else {
component = <p>404 not found</p>;
}
})
return (
<Provider store={store}>
{component}
</Provider>
)
}
}
0
这是一个现实生活中的同构应用https://github.com/WebbyLab/itsquiz-wall与已经解决了所有这些问题。这里介绍
数据人口问题https://github.com/WebbyLab/itsquiz-wall/blob/master/server/app.js
这里是我的关于处理同构问题的博文 - "The Pain and the Joy of creating isomorphic apps in ReactJS"
相关问题
- 1. reactjs反应选择初始数据
- 2. 初始化数据结构
- 3. 贝宝IPN初始和后续反应
- 4. 初始化反应成分
- 5. 反应初始状态。计数
- 6. 反应:父模式初始化获取子数据?
- 7. 结构中的数组和初始化
- 8. 构造和初始化数组
- 9. 构造函数初始化和字典
- 10. 变量初始化和构造函数
- 11. 同时声明和初始化结构/类的数组
- 12. 同构反应 - 校验和不匹配
- 13. 初始化结构数组
- 14. 结构数组初始化
- 15. 结构初始化参数
- 16. 结构数组初始化
- 17. 初始化结构和嵌套结构
- 18. 设置初始值角2反应formarray
- 19. 结构和不清除/初始化所有数据
- 20. 初始化数据
- 21. descontinuous初始数据
- 22. 在构造函数或componentWillMount中设置初始反应组件状态?
- 23. grails应用程序的初始数据
- 24. 内置应用的Django初始数据
- 25. 初始下载应用数据
- 26. MongoDB + NodeJS Web应用程序的初始数据库结构/数据
- 27. 初始化,初始化与参数和便利初始化
- 28. 在构造函数中使用反射和接口来初始化C#
- 29. C++结构体初始值初始化
- 30. 初始化参数不同