【发布时间】:2016-03-26 04:23:28
【问题描述】:
我正在尝试在我的 ReactJs 应用程序中使用 Reflux。
我遇到了一个问题,即侦听数据存储的组件正在空组件实例上执行回调,而不是实例化的实际组件。
在调用操作 toggleUserBar 时,会调用数据存储区并执行触发器。在组件Sidebar中,调用了回调但组件状态为空:
未捕获的类型错误:无法读取未定义的属性“sd”
知道我做错了什么吗?
actions.tsx
var Reflux = require('reflux');
var RQ = {};
RQ.actions = Reflux.createActions([
"toggleUserBar"
]);
window.RQ = RQ;
store.tsx
RQ.store = Reflux.createStore({
listenables: [RQ.actions],
init: function() {},
onToggleUserBar: function() {
this.trigger();
}
});
sidebar.tsx
import * as React from "react";
import * as ReactDOM from "react-dom";
const LeftNav = require('material-ui/lib/left-nav');
const Menu = require('material-ui/lib/menu/menu')
interface ISidebarProps extends React.Props<any> {
}
interface ISidebarState extends React.Props<any> {
shown: Boolean;
sd: any;
}
export class Sidebar extends React.Component<ISidebarProps, ISidebarState> {
unsubscribe: any;
constructor(props: ISidebarProps) {
super(props);
this.state = { shown: true, sd: null };
};
componentDidMount() {
this.unsubscribe = RQ.store.listen(function() {
this.state.sd.setState({ open: true });
});
};
componentWillUnmount() {
this.unsubscribe();
};
render() {
let menuItems = [
{ route: 'get-started', text: 'Get Started' },
{ route: 'customization', text: 'Customization' },
{ route: 'components', text: 'Components' }
];
return (
<LeftNav ref={(c) => this.state.sd = c} docked={false} openRight={true} menuItems={menuItems} />
);
};
}
侧边栏组件在另一个组件中渲染:
<ComSidebar ref={(c) => sdref = c} />
如果我手动调用 sdref.state.sd.open = true,它就可以正常工作。
【问题讨论】:
标签: javascript reactjs refluxjs