I am working off Alex Bank's "Building a Polling App with Socket IO and React.js" (Lynda.com) , but I am trying to upgrade it to react-router 1.0.0-RC1.
Solution below, just skip all this
Please do not send me to documentation, it is not working for me, I guess I must be too thick to understand the documentation's "pithiness".
I have a main APP with 3 child routes, (Audience, Speaker & Board).
My code so far:
APP.js
import React, { Component } from 'react';
import io from 'socket.io-client';
import Header from './parts/Header';
import Routes from '../router/routes';
import { createHistory, useBasename } from 'history';
const history = useBasename(createHistory)({
basename: '/'
});
export default class APP extends Component {
constructor() {
super();
this.state = ({
status: 'disconnected',
title: ''
});
}
componentWillMount() {
this.socket = io('http://localhost:3000');
this.socket.on('connect', this.connect.bind(this));
this.socket.on('disconnect', this.disconnect.bind(this));
this.socket.on('welcome', this.welcome.bind(this));
}
connect() {
this.setState({status: 'connected'});
}
disconnect() {
this.setState({status: 'disconnected'});
}
welcome(serverState) {
this.setState({title: serverState.title});
}
render() {
return (
<div>
<Header title={ this.state.title } status={ this.state.status }/>
{ /* I WANT TO PASS THIS.STATE.STATUS TO CHILD ROUTES */}
<Routes history={ history } />
</div>
);
}
}
Routes.js
import React, { Component } from 'react';
import Route from 'react-router';
import APP from '../components/APP';
import Audience from '../components/Audience';
import Board from '../components/Board';
import Speaker from '../components/Speaker';
import NotFound from '../components/NotFound';
export default class Routes extends Component {
constructor() {
super();
}
render() {
return (
<Route history={ this.props.history } component={ APP }>
<Route path="/" component={ Audience } />
<Route path="audience" component={ Audience } />
<Route path="board" component={ Board } />
<Route path="speaker" component={ Speaker } />
<Route path="*" component={ NotFound } />
</Route>
);
}
}
Audience.js
import React, { Component } from 'react';
export default class Audience extends Component {
constructor() {
super();
}
render() {
return (
<div>
Audience - STUCK HERE!! - How to pass APP's this.state.status as a prop????
</div>
);
}
}
Although the app runs, and I have read the documentation, I am still unable to pass APP's this.state.status as a property to the Audience app.
I have been at this for 2 days to no avail and it is becoming frustrating. TGIF.
Desired Result:
When a browser is opened to localhost:3000, the default page (Audience.js), should read as:
Untitled Presentation - connected Audience - connected
I cannot get the status of connected passed to the Audience component so the word 'connected' is not showing up next to Audience. I am connected as evidenced by Header's "Untitled Presentation - connected"
Can someone assist me here.
Many Thanks!