gpt4 book ai didi

javascript - 从存储调用 api 并将数据绑定(bind)到 ReactJS/flux 中的组件状态

转载 作者:行者123 更新时间:2023-11-28 15:25:58 25 4
gpt4 key购买 nike

使用 React-router-component,我设置使用名为 gigist 的组件加载我的主页,如下所示:

var React = require('react');
var AppStore =require('../../stores/app-store.js');
var GigsListItem = require('./giglistitem.js');
var StoreWatchMixin =require('../../mixins/StoreWatchMixin.js');

function getGigs()
{

return {items:AppStore.getGigs()}
}

var GigsList= React.createClass({
mixins:[new StoreWatchMixin(getGigs)],
componentWillMount:function()
{
this.setState(getGigs());
console.log(this.state);
},
componentDidMount:function()
{
this.setState(getGigs());
console.log(this.state);
},
render:function()
{
console.log("rendered view");
var liststyle={border:'1px solid black'};

/*
var items=this.state.items.map(function(item)
{
return <GigsListItem gig={item} />
}) */
return (

<div className="row" style={liststyle}>
{this.state.items}

</div>
)
}
});

module.exports=GigsList;

我正在使用 npm reqwest 进行 ajax 调用,但问题是在 ajax 请求完成之前正在渲染 View 并将其状态设置为未定义。你可以从我的控制台日志中看到它 From the consoe '1' is at setting initial state for component giglist,'3' is component will mount,'rendered view' is while rendering component,'2' is for the ajax request

从控制台中,'1'是设置组件giglist的初始状态,'3'是组件将挂载,'渲染 View '是渲染组件时,'2'是用于ajax请求

它清楚地表明在 ajax 调用之后数据没有被设置为组件状态。

在收到组件的响应后,我需要设置组件的状态。我尝试使用 componentwillmount 和 componentdidmount 设置状态,但都没有解决我的问题。

这是我的商店代码:

var AppDispatcher = require('../dispatchers/app-dispatcher');
var AppConstants = require('../constants/app-constants');
var reqwest = require('reqwest');
var merge = require('react/lib/merge');
var EventEmitter = require('events').EventEmitter;

var CHANGE_EVENT = "change";

var _gigs = [];

function getGigsList()
{

reqwest({url:'myapi',
type:'get',
success:function(resp)
{
console.log(2);
return resp.gigs;
//alert(JSON.stringify(_gigs));
}.bind(this)})


}



var _cartItems = [];


function _removeItem(index){
_cartItems[index].inCart = false;
_cartItems.splice(index, 1);
}

function _increaseItem(index){
_cartItems[index].qty++;
}

function _decreaseItem(index){
if(_cartItems[index].qty>1){
_cartItems[index].qty--;
}
else {
_removeItem(index);
}
}


function _addItem(item){
if(!item.inCart){
item['qty'] = 1;
item['inCart'] = true;
_cartItems.push(item);
}
else {
_cartItems.forEach(function(cartItem, i){
if(cartItem.id===item.id){
_increaseItem(i);
}
});
}
}

function _CartTotals()
{
var qty=0,total=0;
_cartItems.forEach(function(cartItem)
{
qty+=cartItem.qty;
total+=cartItem.cost * cartItem.qty;

});
return {'qty':qty,'total':total}
}


var AppStore = merge(EventEmitter.prototype, {
emitChange:function(){
this.emit(CHANGE_EVENT)
},

addChangeListener:function(callback){
this.on(CHANGE_EVENT, callback)
},

removeChangeListener:function(callback){
this.removeListener(CHANGE_EVENT, callback)
},

getCart:function(){
return _cartItems
},

getGigs:function(){
alert(JSON.stringify(getGigsList()));
return getGigsList();
},

getCartTotals:function()
{
return _CartTotals();
},

dispatcherIndex:AppDispatcher.register(function(payload){
var action = payload.action; // this is our action from handleViewAction
switch(action.actionType){
case AppConstants.ADD_ITEM:
_addItem(payload.action.item);
break;

case AppConstants.REMOVE_ITEM:
_removeItem(payload.action.index);
break;

case AppConstants.INCREASE_ITEM:
_increaseItem(payload.action.index);
break;

case AppConstants.DECREASE_ITEM:
_decreaseItem(payload.action.index);
break;
}
AppStore.emitChange();

return true;
})
})

module.exports = AppStore;

有人可以指出我做错的地方吗?我希望我很清楚。提前致谢

最佳答案

您不能在异步代码中使用 return 语句,即在 ajax 调用的成功回调中。

相反,尝试检查商店中是否已经有演出,在这种情况下您将返回它们,如果商店中没有演出,您会请求获取一些演出,并在成功回调中设置商店状态到新数据并发出一个更改,告诉您的组件尝试再次获取存储状态。

    function getGigsList()
{
if (_gigs.length > 0) { return _gigs; } //return immediately if data exists

reqwest({url:'myapi',
type:'get',
success:function(resp)
{
console.log(2);
_.gigs = resp.gigs;
this.emit(CHANGE_EVENT);
}.bind(this)})

return _gigs; //currently an empty array. this way you don't have to check for undefined in the component, you can just iterate over it directly
}

关于javascript - 从存储调用 api 并将数据绑定(bind)到 ReactJS/flux 中的组件状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28950213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com