【发布时间】:2017-06-17 10:45:42
【问题描述】:
我正在利用 Flux 架构在 Reactjs 中进行开发。当我添加视频时,我得到了错误,但是当我刷新页面时,视频确实出现了。所以我在控制台中收到了这个错误:
Uncaught TypeError: Cannot read property 'map' of undefined
而且我相信错误可能出在这个文件 components/App.js 的某个地方:
var React = require('react');
var AppActions = require('../actions/AppActions');
var AppStore = require('../stores/AppStore');
var AddForm = require('./AddForm');
var VideoList = require('./VideoList');
function getAppState(){
return {
videos: AppStore.getVideos()
}
}
var App = React.createClass({
getInitialState: function(){
return getAppState();
},
componentDidMount: function(){
AppStore.addChangeListener(this._onChange);
},
componentUnmount: function(){
AppStore.removeChangeListener(this._onChange);
},
render: function(){
console.log(this.state.videos);
return (
<div>
<AddForm />
<VideoList videos = {this.state.videos} />
</div>
)
},
// Update view state when change is received
_onChange: function(){
this.setState(getAppState());
}
});
module.exports = App;
但您可能还需要在 components/VideoList.js 中查看此文件:
var React = require('react');
var AppActions = require('../actions/AppActions');
var AppStore = require('../stores/AppStore');
var Video = require('./Video');
var VideoList = React.createClass({
render: function(){
return (
<div className="row">
{
this.props.videos.map(function(video, index){
return (
<Video video={video} key={index} />
)
})
}
</div>
);
}
});
module.exports = VideoList;
这是我的商店/AppStore.js 文件:
var AppDispatcher = require('../dispatchers/AppDispatcher');
var AppConstants = require('../constants/AppConstants');
var EventEmitter = require('events').EventEmitter;
var assign = require('object-assign');
var AppAPI = require('../utils/AppAPI');
var CHANGE_EVENT = 'change';
var _videos = [];
var AppStore = assign({}, EventEmitter.prototype, {
saveVideo: function(video){
_videos.push(video);
},
getVideos: function(){
return _videos;
},
setVideos: function(videos){
_videos = videos;
},
emitChange: function(){
this.emit(CHANGE_EVENT);
},
addChangeListener: function(callback){
this.on('change', callback);
},
removeChangeListener: function(){
this.removeListener('change', callback);
}
});
AppDispatcher.register(function(payload){
var action = payload.action;
switch (action.actionType) {
case AppConstants.SAVE_VIDEO:
console.log('Saving Video...');
// Store SAVE
AppStore.saveVideo(action.video);
// API SAVE
AppAPI.saveVideo(action.video);
// Emit change
AppStore.emit(CHANGE_EVENT);
case AppConstants.RECEIVE_VIDEOS:
console.log('Saving Video...');
// Set Videos
AppStore.setVideos(action.videos);
// Emit change
AppStore.emit(CHANGE_EVENT);
}
return true;
});
module.exports = AppStore;
我不确定如何解决这个问题。我查看了一些文档,但无法找到问题的答案。
【问题讨论】:
-
console.log(this.state.videos)给你什么? -
@putvande,我得到:未捕获的类型错误:无法读取未定义的属性“状态”
-
尝试将
function getAppState放在app component中并使用this.getAppState()而不是getAppState()调用它,然后让我们知道您在控制台中看到的内容。 -
在
AppDispatcher中添加console.log(action)下方action。它返回什么?你在一个地方有action.video,在另一个地方有action.videos。只是想知道对象是否以某种方式命名不同。 -
@RandomUser,这是我在控制台中得到的:模块构建失败:SyntaxError: Unexpected token, expected, (14:11) 12 | 13 | var App = React.createClass({ > 14 | function getAppState(){ | ^ 15 | return { 16 | 视频: AppStore.getVideos() 17 | } BabelLoaderError: SyntaxError: Unexpected token, expected , (14:11) 12 | 13 | var App = React.createClass({ > 14 | function getAppState(){ | ^ 15 | return { 16 | 视频:AppStore.getVideos() 17 | }
标签: reactjs reactjs-flux