【问题标题】:Running a Array of Objects outside render function在渲染函数之外运行对象数组
【发布时间】:2020-01-22 10:56:12
【问题描述】:

在componentDidMount 中编译定义变量都失败。我做了很多其他方法。似乎没有一个适用于我的特定代码。我认为阅读比试图解释更好。

import React from 'react';
import { connect } from './api';
import './App.css';

class App extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      giphy: []
    }
  }

  componentDidMount(){
    connect(message => {
      this.setState({
        giphy: message
      })
    });
    var Items = this.state.giphy.map(function(gif){ // items is not defined.
      return <li>{gif}</li>;
    })
  }

  render () {
      return (
        <div className=".App-logo">
            <ul>
              { Items } // I wanted to show all items inside the array of objects.
            </ul>

            <ul className=".App-logo"> 
            // the following method works. We need to make sure to check for this conditions or wont work
              {this.state.giphy && this.state.giphy.length > 0   &&
                <img src={ this.state.giphy[2].images.original.url}
                alt="giphy.com animations"/>}
            </ul>
      </div>
    )
  }
}

如果我删除项目,它将显示状态中的第二个项目。 你能帮忙展示一下状态吗?

【问题讨论】:

  • 您需要在 setState 上使用回调,您的数组未填充。 `setState({}, () => { 此处映射状态}).
  • 您好,请检查我的解决方案,如果有帮助,请告诉我。

标签: arrays reactjs javascript-objects


【解决方案1】:

您可以在render 方法中直接map 您的状态,而不是在componentDidMount 中创建一个不能在render 方法中使用的变量。

<ul>
   //This will show only `bitly_gif_url`
   {Array.isArray(this.state.giphy) && this.state.giphy.map(gif => <li>{gif.bitly_gif_url}</li>) } 
</ul>

注意:您的giphy 数组包含多个对象。在每个对象中,我使用{gif.bitly_gif_url} 仅显示了bitly_gif_url,如果您需要显示对象中的任何其他项目,您可以更改它的键。

您也可以一次显示多个项目,

<ul>
   //This will show `bitly_gif_url` and `embed_url` at a time
   {Array.isArray(this.state.giphy) && this.state.giphy.map(gif => <li>{gif.bitly_gif_url} {gif.embed_url}</li>) } 
</ul>

【讨论】:

  • @DanielSmith,一定会调查的。
【解决方案2】:

由于您在 componentDidMount 函数中定义了 Items,它具有功能范围,并且在 render 函数中不可用,您可以做的是从函数中返回项目。所以现在你的代码看起来像

import React from 'react';
import { connect } from './api';
import './App.css';

class App extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      giphy: []
    }

  }

  componentDidMount(){
    connect(message => {
      this.setState({
        giphy: message
      })
    });
  }

  getItems() {
    return this.state.giphy.map(function(gif){
      return <li>{gif}</li>;
    })
  }

  render () {
      return (
        <div className=".App-logo">
            <ul>
              { this.getItems() } // I wanted to show all items inside the array of objects.
            </ul>

            <ul className=".App-logo"> 
            // the following method works. We need to make sure to check for this conditions or wont work
              {this.state.giphy && this.state.giphy.length > 0   &&
                <img src={ this.state.giphy[2].images.original.url}
                alt="giphy.com animations"/>}
            </ul>
      </div>
    )
  }
}

【讨论】:

  • 对象作为 React 子对象无效(发现:对象带有键 {type, id, slug, url, bitly_gif_url, bitly_url, embed_url, username, images})。如果您打算渲染一组子项,请改用数组。
  • 因为地图函数中的 gif 是一个对象,您需要将 {gif} 替换为 {} 我想你明白了。
猜你喜欢
  • 1970-01-01
  • 2021-10-16
  • 2019-11-15
  • 2019-04-23
  • 2018-09-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多