【发布时间】:2015-07-26 22:23:09
【问题描述】:
我正在尝试通过创建一个 notes webapp 来测试 React 功能。在左侧边栏中,我列出了当前登录用户的所有笔记。选择其中一个注释时,我希望它显示在网页的主区域。
这在我第一次选择笔记时有效,但在此之后无效,除非我手动刷新页面。我认为组件是第一次安装并检索注释,但是当我选择另一个注释时,它不会更新组件。
我正在使用 react-router,所以当我选择一个笔记时,它会根据所选笔记遍历到 /notes/:note_id。这总是会改变,但内容不会刷新。
这是我的 NoteItem 代码:
var React = require('react');
var WebAPIUtils = require('../../utils/WebAPIUtils.js');
var NoteStore = require('../../stores/NoteStore.react.jsx');
var NoteActionCreators = require('../../actions/NoteActionCreators.react.jsx');
var Router = require('react-router');
var State = require('react-router').State;
var NoteItem = React.createClass({
mixins: [ State ],
getInitialState: function() {
return {
note: NoteStore.getNote(),
errors: []
};
},
componentDidMount: function() {
NoteStore.addChangeListener(this._onChange);
NoteActionCreators.loadNote(this.getParams().noteId);
},
componentWillUnmount: function() {
NoteStore.removeChangeListener(this._onChange);
},
_onChange: function() {
this.setState({
note: NoteStore.getNote(),
errors: NoteStore.getErrors()
});
},
render: function() {
return (
<div className="row">
<div className="note-title">{this.state.note.title}</div>
<div className="note-body">{this.state.note.body}</div>
</div>
);
}
});
module.exports = NoteItem;
这是我使用 react-router 从侧边栏中链接到 NoteItem 的方式:
<Link to="note" params={ {noteId: this.props.note.id} }>{this.props.note.title}</Link>
还有我的路线:
<Route name="app" path="/" handler={NotesApp}>
<DefaultRoute />
<Route name="login" path="/login" handler={LoginPage}/>
<Route name="signup" path="/signup" handler={SignupPage}/>
<Route name="notes" path="/notes" handler={NotesSidebar}/>
<Route name="note" path="/notes/:noteId" handler={NoteItem} />
<Route>
任何帮助将不胜感激。
谢谢!
【问题讨论】:
标签: javascript reactjs reactjs-flux react-router