我正在使用ReactJS中的實時儀表板應用程序來監視傳感器。我在PHP上使用AutobahnJS + Websockets來傳輸數據。ReactJS:傳輸道具
這是我的儀表板在組件視圖中的抽象。 Abstraction of dashboard in component view
Main.jsx:
class Main extends React.Component {
constructor(props) {
super(props)
}
componentDidMount() {
$(document).foundation();
var that = this;
var conn = new ab.Session('ws://xx.xxx.xxx.xx', function() {
conn.subscribe('', function(topic, data) {
console.log(data);
that.setState({
result: data
});
});
}, function() {
console.warn('WebSocket connection closed');
}, {'skipSubprotocolCheck': true});
}
render() {
return (
<div>
<div className="off-canvas-wrapper">
<div className="off-canvas-wrapper-inner" data-off-canvas-wrapper>
<div className="off-canvas position-right" data-position="right" id="offCanvas" data-off-canvas>
<SensorDetails/>
</div>
<div className="off-canvas-content" data-off-canvas-content>
<Nav/>
<div className="row">
<div className="columns medium-12 large 12">
{this.props.children}
</div>
</div>
</div>
</div>
</div>
</div>
);
}
};
module.exports = Main;
什麼是道具傳遞從Main.jsx到BuildingList.jsx的正確方法?我曾嘗試更換:
{this.props.children}
與
<Dashboard data={this.state.result}/>
這工作,但我無法訪問例如我的鏈接帳號設定。我的反應路由器設置如下:
<Router history={hashHistory}>
<Route path="/dashboard" component={Main} >
<Route path="/about" component={About} onEnter={requireLogin}/>
<Route path="/examples" component={Examples} onEnter={requireLogin}/>
<Route path="/accountSettings" component={AccountSettings} onEnter={requireLogin}/>
<IndexRoute component={Dashboard}/>
</Route>
<Route path="/" component={Login} onEnter={redirectIfLoggedIn}/>
</Router>
我該如何解決這個問題?謝謝。
謝謝Enjijar!我已經嘗試了兩種解決方案,但都沒有工作。對於第二種解決方案,我收到「應用程序未定義」錯誤。 –