this.setState()不会与特定对象一起执行

时间:2019-05-31 17:32:06

标签: reactjs react-native reactive-programming setstate

我不知道发生了什么,希望一些新鲜的眼睛会有所帮助。这可以正常工作好几天,但现在却没有任何变化。

我有2个对象-gamesImagesgamePlaygamePlay是包含多个联接表的记录。 gameImages是10个不同的记录。

我打个电话:

matchingGameApiHelper.getGamePlay(this.props.currentUser.authToken, data)
  .then(data => {
      let gameData = data.data
      console.log(gameData.data.gameImages)
      //output -> (10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]

      this.setState({
        activeGame: true,
        room: room,
        gamePlay: gameData.data.gamePlay,
        gameImages: gameData.data.gameImages,
      }, () => console.log('setState is done'))
})
.catch(error => {
    console.log(error)
})

删除gameImages: gameData.data.gameImages行后,它可以正常工作。有了它,它就不会更新状态,也不会引发错误。它基本上只是停止,之后什么也不运行。我还尝试保存一种类似gameImages: gameData.data.gameImages[0]的文件,并且效果很好。

更新:这是初始状态

this.state = {
  activeGame: '',
  room: '',
  gamePlay: [],
  gameImages: [],
 }

我也尝试过gameImages: {},

UPDATE2在此处是gameImages的扩展输出:

(10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {id: 1, image_file_name: "adult-afro-beautiful-1858175.jpg", exchange_id: 58, premium: false, created_at: "2019-04-30T22:44:54.382Z", …}
1: {id: 2, image_file_name: "adult-asian-blue-936593.jpg", exchange_id: 58, premium: false, created_at: "2019-04-30T22:46:41.291Z", …}
2: {id: 3, image_file_name: "adult-attractive-beautiful-1197132_(1).jpg", exchange_id: 58, premium: false, created_at: "2019-04-30T22:47:06.869Z", …}
3: {id: 4, image_file_name: "adult-attractive-casual-247885.jpg", exchange_id: 58, premium: false, created_at: "2019-04-30T22:47:41.877Z", …}
4: {id: 5, image_file_name: "adult-beach-casual-736716.jpg", exchange_id: 58, premium: false, created_at: "2019-04-30T22:47:55.862Z", …}
5: {id: 6, image_file_name: "adult-beard-cute-1250426.jpg", exchange_id: 58, premium: false, created_at: "2019-04-30T22:48:15.063Z", …}
6: {id: 7, image_file_name: "adult-beautiful-brunette-1181686.jpg", exchange_id: 58, premium: false, created_at: "2019-04-30T22:48:46.868Z", …}
7: {id: 8, image_file_name: "adult-beautiful-cute-1520760.jpg", exchange_id: 58, premium: false, created_at: "2019-04-30T22:49:29.462Z", …}
8: {id: 9, image_file_name: "adult-beautiful-cute-1895160.jpg", exchange_id: 58, premium: false, created_at: "2019-04-30T22:49:47.166Z", …}
9: {id: 10, image_file_name: "adult-blue-boy-1438275.jpg", exchange_id: 58, premium: false, created_at: "2019-04-30T22:50:10.189Z", …}
length: 10
__proto__: Array(0)

这是返回的原始数据:

data:
  gameImages: (10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
  gamePlay: {id: 482, player_one: 32, player_two: null, room: "LEX305", exchange_id: 58, …}
__proto__: Object 

0 个答案:

没有答案