【问题标题】:How to display other text based on data getting from server in reactjsreactjs 如何根据从服务器获取的数据显示其他文本
【发布时间】:2019-07-17 20:39:49
【问题描述】:

如何根据从服务器获取的数据显示其他文本或图标,这里我从服务器获取以下这些数据,对于所有 false 参数,我想显示一些其他文本而不是 false,如果有 false 那么我想要显示一些十字图标,如果有,那么我想显示成功图标, 这是我的数据的屏幕截图

这是我展示数据的功能

showPrice= () => {
debugger;
if (this.state.priceList !== undefined) {
  return this.state.priceList.map(price => {


    return (

      <tr>
        <td>{price.premiumname}</td>
        <td>dfdf</td>
        <td>{price.expired_time}</td>
        <td>{price.inrprice}</td>

    <td>{price.entertainmentvideo }</td>
        </td>}
        <td>{price.accesslesson}</td>
        <td>{price.exerciselesson}</td>
        <td>{price.lsystem}</td>
        <td>{price.vocabulary}</td>
        <td>{price.materialdownload}</td>
        <td>{price.exclusivewebinars}</td>
        <td>{price.tutoring}</td>
        <td>{price.conversation}</td>
        <td>{price.subscribtion}</td>
        <td>

请帮助我,我是 Reacts 新手 谢谢

【问题讨论】:

  • 提供错误信息。你在更新渲染方法中的状态吗?
  • 您的错误告诉您您尝试更新状态的次数已达到限制。我猜你正在尝试在每次渲染时更新它?
  • 您的render 方法内容存在问题,它包含对在没有控制器的情况下调用并使用setState 的函数的调用。这会导致状态在安装组件的第一个周期内多次呈现,验证您的 render 方法,并找出导致此行为的函数,如果您使用组件的其余代码编辑问题,我们可能会能够提供帮助。

标签: reactjs hide show


【解决方案1】:

让我们假设以下状态,在componentDidMount 中的 API 调用后获取更新:

state = { data : null }

componentDidMount(){
     APICall().then(data => this.setState({ data })) 
}

现在你只需要根据状态的属性来渲染文本。例如:

render(){
    <>
        !this.state.data ? <p>Loading</p> : <p>Loaded</p>
    </> 
}  

【讨论】:

  • 是的,我只需要根据属性显示数据
  • {this.state.entertainmentvideo === "false" ? &lt;td&gt;&lt;i class="ti-trash"&gt;&lt;/i&gt;&lt;/td&gt;:&lt;td&gt;&lt;i class="ti-close"&gt;&lt;/i&gt;&lt;/td&gt;}
  • 如果 entertainmentvide ="false" 那么我想显示一些文本 if =="true" 然后我想显示一些其他文本
猜你喜欢
  • 1970-01-01
  • 2020-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-12
相关资源
最近更新 更多