客户端的React(Isomorphic)链接不呈现路由

时间:2017-04-17 01:57:58

标签: javascript node.js reactjs react-router isomorphic-javascript

所以,我一直在努力解决这个问题。

工作原理:应用程序在服务器端加载正常。 routes / index.jsx(这是我的服务器端路由文件)加载页面就好了。 console.log('News');在客户端javascript控制台和页面加载时服务器的节点日志上显示News的日志。我最初可以加载任何页面。

问题:一旦页面加载,我点击链接,在标题中,URL栏中的地址会发生变化,但UI不会更新,如果我没有console.log('Home')我要导航到Home,反之亦然,如果我要导航到News我不会在客户端和服务器上发生console.log('News');

我有以下文件。

路由/ Index.jsx - 服务器端渲染

var router = require('express').Router();
var React = require('react');
var ReactDOMServer = require('react-dom/server');
var StaticRouter = require('react-router-dom').StaticRouter;
var Redux = require('redux');
var Provider = require('react-redux').Provider;
var App = require('../views/app.jsx');

function reducer(state) { return state; }

router.get('*', function(req, res) {
  var initialState = { title: 'Home' };
  const store = Redux.createStore(reducer, initialState);
  var context = {};

  var html = ReactDOMServer.renderToString(
      <Provider store={store}>
        <StaticRouter location={req.url} context={context}>
          {require('../views/app.jsx')}
        </StaticRouter>
      </Provider>
    );

  var windowprops = store.getState();
  windowprops = JSON.stringify(windowprops).replace(/</g, '\\u003c');
  var frame = '<!DOCTYPE html><html><head><link rel="stylesheet" type="text/css" href="/css/layout.css" /><title>'+store.getState().title+' | Lunar Realm</title></head><body><div id="root">'+html+'</div><script>window.PROPS = '+windowprops+'</script><script src="/bundle.js"></script></body></html>';

  if(context.status >= 400) {
    res.status(context.status).send(frame);
  } else if (context.url) {
    res.redirect(context.status, context.url);
  } else {
    res.send(frame);
  }
});
module.exports = router;

视图/ app.jsx

var React = require('react');
var ReactRouter = require('react-router-dom');
var Route = ReactRouter.Route;
var Layout = require('../views/Layout.jsx');
var Redirect = ReactRouter.Redirect;
var Switch = ReactRouter.Switch;

module.exports = (
    <Layout>
      <Switch>
        <Route exact path='/' component={require('./Index.jsx')} />
        <Route exact path='/news' component={require('./News.jsx')} />
      </Switch>
    </Layout>
);

Client.JS - 适用于WebPack

var ReactDOM = require('react-dom');
var React = require('react');
var Routes = require('./routes/routes.jsx');
var Redux = require('redux');
var Provider = require('react-redux').Provider;

function reducer(state) { return state; }

var store = Redux.createStore(reducer, window.PROPS);

ReactDOM.render(
    <Provider store={store}>
        {Routes}
    </Provider>, document.getElementById('root')
);

Routes.JSX - 用于前端路由

var React = require('react');
var ReactRouter = require('react-router-dom');
var BrowserRouter = ReactRouter.BrowserRouter;
var Router = ReactRouter.Router;
var Route = ReactRouter.Route;
var Redirect = ReactRouter.Redirect;
var Switch = ReactRouter.Switch;
var Layout = require('../views/Layout.jsx');

import createHistory from 'history/createBrowserHistory';
var history = createHistory();

module.exports = (
  <BrowserRouter>
    <Layout>
      <Switch>
        <Route exact path="/" component={require('../views/Index.jsx')} />
        <Route exact path='/news' component={require('../views/News.jsx')} />
        <Route component={require('../views/404.jsx')} />
      </Switch>
    </Layout>
  </BrowserRouter>
);

views / News.jsx - 布局的孩子

var React = require('react');

var News = React.createClass({
  render: function(){
    console.log('news');
    return(
      <div id="canvas-section-news" className="canvas-section">
        News
      </div>
    );
  }
});

module.exports = News;

任何人都可以帮我弄清楚为什么我的UI不会更新?我应该注意,我的节点日志和浏览器的javascript控制台都没有出现任何错误。

我的package.json中的依赖项

"dependencies": {
    "babel-loader": "^6.4.1",
    "babel-plugin-transform-class-properties": "^6.23.0",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-polyfill": "^6.23.0",
    "babel-preset-es2015": "^6.24.0",
    "babel-preset-react": "^6.23.0",
    "babel-preset-stage-0": "^6.22.0",
    "babel-register": "^6.24.0",
    "express": "^4.15.2",
    "history": "^4.6.1",
    "mongoose": "*",
    "react": "^15.4.2",
    "react-dom": "^15.4.2",
    "react-redux": "^5.0.3",
    "react-router": "^4.0.0",
    "react-router-dom": "^4.0.0",
    "redux": "^3.6.0",
    "webpack": "^2.3.3"
  }

1 个答案:

答案 0 :(得分:0)

好的,我明白了。我如何使用React Connect()函数存在问题。确保你正确使用它!