Sunday, 16 April 2017

React (Isomorphic) Link Not Rendering Route on Client Side

So, I've been struggling with this for days.

What Works: The application loads just fine on the server-side. routes/index.jsx (which is my server-side routing file) loads the page just fine. The console.log('News'); shows a log of News on both the client javascript console and the server's node logs on page load. I can load any page initially.

The Problem: Once the page loads, and I click on a link, in the header, the address in the URL bar changes, but the UI doesn't update and I don't get a console.log('Home') if I were to navigate to the Home, vice-versa if I were to navigate to News I wouldn't get a console.log('News'); happening on either the client, nor the server.

I have the following files.

Routes/Index.jsx - Server Side Rendering

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;

views/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 - For 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 - For Front-End Routing

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 - A Child of the Layout

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;

Could anyone help me figure out why my UI doesn't update? I should note that I don't get any errors in my node logs nor, the browser's javascript console.

The Dependencies in my 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"
  }



via Devontrae

No comments:

Post a Comment