我正在研究Alex Bank 的“使用 Socket IO 和 React.js 构建轮询应用程序”(Lynda.com),但我正在尝试将其升级到 react-router 1.0.0-RC1。
下面的解决方案,跳过所有这些
请不要将我发送到文档,它对我不起作用,我想我一定太厚了,无法理解文档的“精髓”。
我有一个带有 3 条子路线(观众、演讲者和董事会)的主应用程序。
到目前为止我的代码:
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>
);
}
}
路由.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>
);
}
}
尽管应用程序运行,并且我已阅读文档,但我仍然无法将 APP 的 this.state.status 作为属性传递给 Audience 应用程序。
我已经在这 2 天无济于事,这变得令人沮丧。动图。
期望的结果:
当浏览器打开到 localhost:3000 时,默认页面 (Audience.js) 应如下所示:
Untitled Presentation - connected Audience - connected
我无法将已连接状态传递给受众组件,因此“已连接”一词未显示在受众旁边。Header 的“Untitled Presentation - connected”证明了我已连接
有人可以在这里帮助我。
非常感谢!