2015-12-28 8 views
6

Sto configurando una semplice app giocattolo per imparare React/Hapi e tutto funziona bene fino a quando non provo a impostare up routing serveride. Il server funziona senza errori e restituisce "/" correttamente con Hello World.Errore di rendering serveride di React Router: Avviso: propType non riuscito: prop `history` non specificato in` RoutingContext`

Tuttavia quando navigo in "/ test" ottengo i seguenti errori.

Warning: Failed propType: Required prop `history` was not specified in `RoutingContext`. 
Warning: Failed propType: Required prop `location` was not specified in `RoutingContext`. 
Warning: Failed propType: Required prop `routes` was not specified in `RoutingContext`. 
Warning: Failed propType: Required prop `params` was not specified in `RoutingContext`. 
Warning: Failed propType: Required prop `components` was not specified in `RoutingContext`. 

Dove sto andando storto qui?

Server.js

'use strict'; 

const Hapi = require('hapi'); 
const Path = require('path'); 

const server = new Hapi.Server(); 
server.connection({ port: 3000}); 

//React Junk 
import React from 'react'; 
import {createStore} from 'redux'; 
import {Provider} from 'react-redux'; 
import { renderToString } from 'react-dom/server'; 
import reducer from './../common/Reducers/index.js'; 
import { match, RoutingContext } from 'react-router'; 
import Routes from './../common/routes/Routes.js'; 

const handleRender = function(req, res) { 
    const store = createStore(reducer); 
    match({Routes, location: req.url}, (error, redirectLocation, renderProps) => { 
     //res(req.url); 
     if(error) { 
      res(error.message); 
     } 
     else { 
      const html = renderToString(
      <Provider store={store}> 
       <RoutingContext {...renderProps} /> 
      </Provider> 
      ); 

      const initialState = store.getState(); 

      res(renderFullPage(html, initialState)); 
     } 

    }); 
    // const html = renderToString(
    // <Provider store={store}> 
    //  <App /> 
    // </Provider> 
    //); 

    // const initialState = store.getState(); 

    // res(renderFullPage(html, initialState)); 
} 

const renderFullPage = function(html, initialState) { 
    return ` 
     <!doctype html> 
     <html> 
      <head> 
       <title>Please Work</title> 
      </head> 
      <body> 
       <div id="app-mount">${html}</div> 
       <script> 
        window.__INITIAL_STATE__ = ${JSON.stringify(initialState)} 
       </script> 
       <script src="/static/bundle.js"></script> 
      </body> 
     </html> 
    `; 
} 

server.register(require('inert'), (err) => { 
    server.route({ 
     method: 'GET', 
     path: '/static/{filename}', 
     handler: function (req, reply) { 
      reply.file('static/' + req.params.filename); 
     } 
    }) 
    server.route({ 
     method: 'GET', 
     path: '/', 
     handler: function(req, res) { 
      res('hello world'); 
     } 
    }); 
    server.route({ 
     method: 'GET', 
     path: '/{path*}', 
     handler: function(req, res) { 
      handleRender(req, res); 
     } 
    }) 

    server.start(() => { 
     console.log('Server running at:', server.info.uri); 
    }) 
}) 

Routes.js

import { Route } from 'react-router'; 

//Components 
import App from './../components/App.jsx'; 
import Name from './../components/Name.jsx'; 

export default (
    <Route path="/" component={App}> 
     <Route path="test" component={Name} /> 
    </Route> 
); 

Perché è stato chiesto per

client entry.jsx

import React from 'react'; 
import ReactDOM from 'react-dom'; 

import {createStore} from 'redux'; 
import {Provider} from 'react-redux'; 
import App from './../common/components/App.jsx'; 
import Router from './../common/routes/Router.jsx'; 
import reducers from './../common/Reducers'; 

const initialState = window.__INITIAL_STATE__; 
const store = createStore(reducers(initialState)); 

ReactDOM.render(
    <Provider store={store}> 
     <Router /> 
    </Provider> 
, document.getElementById('app-mount')); 

Client Router

import React, {Component} from 'react'; 
import ReactDOM from 'react-dom'; 
import { Router, Route } from 'react-router'; 
import createHashHistory from 'history/lib/createHashHistory'; 

const history = createHashHistory(); 

import Routes from './Routes.js'; 

export default (
    <Router history={history}> 
     <Routes /> 
    </Router> 
); 
+0

cosa fa il tuo lato client di rendering di codice assomigliare? Questi errori sono errori sul lato client? O errori lato server? – Brandon

+0

Ho aggiunto Client Entry e Client Router al post. Credo che siano server side? Anche se ho questo errore nella mia console Attenzione: React.createElement: tipo non dovrebbe essere nullo, non definito, booleano, o un numero. Dovrebbe essere una stringa (per elementi DOM) o una ReactClass (per componenti compositi). – trattles

risposta

3

È necessario passare history come un puntello per Router sul client:

export default (
    <Router history={history}> 
     <Routes /> 
    </Router> 
); 

Il problema probabilmente con il server del codice è che non sta passando le rotte per match correttamente. Si aspetta una proprietà denominata routes, non Routes. Prova questo:

match({routes: Routes, location: req.url}, (error, redirectLocation, renderProps) => { 

notare Soprattutto questa dichiarazione dal documentation:

If all three parameters are undefined, this means that there was no route found matching the given location. 
+0

In questo caso, ricevo ancora gli stessi errori: Avviso: propType non riuscito: prop 'history' non specificato in' RoutingContext'. Avviso: propType non riuscito: il prop richiesto 'location' non è stato specificato in' RoutingContext'. Avviso: propType non riuscito: prop propost 'route' non è stato specificato in' RoutingContext'. Attenzione: propType non riuscito: il parametro richiesto 'params' non è stato specificato in' RoutingContext'. Avviso: propType non riuscito: prop 'component' non è stato specificato in' RoutingContext'. – trattles

+0

nella console del browser o sul tuo server? – Brandon

+0

Si tratta di errori lato server. Sembra che il routing sul server non stia passando nulla per i renderprops? – trattles

Problemi correlati