如何遍历异步返回的对象并将其显示在react.js中?

山姆

我在显示函数调用结果时遇到麻烦等待有毒(“有毒的句子”)。我能得到的最接近的结果是在react插件中以组件的状态查看此更新,但是它没有更新组件本身。

function ToxicLabels(theObject) {
  return (
  <h2>{theObject.label}  match {theObject.results[0].match}</h2>
  )
}

class App extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      objects: [],
    }
  }

  componentDidMount() {
    this.renderThePost()
  }
  componentDidUpdate(){
    render()
  }

  renderThePost = async () => {
    try {
      let response = await toxic('you suck')
      this.setState({
        object: response,
      })
      // this.state.object.map((object)=>{
      //   console.log(object)
      //   ToxicLabels(object)
      // })
      }catch (err) {
        console.log(err)
      }
    }

  render() {
      return (
        <div>
          <h2>Hello {"Hola"}</h2>
          {this.state.object.map((object)=> {
            console.log(object)
            ToxicLabels(object)
            })}
        </div>
      )
    }
  }

返回的数据结构。这就是TensorFlow的毒性模型返回数据的方式。数组中有7个对象。在每个对象内都有一个标签和一个结果数组,显示匹配(对或错)和概率。

{
  "object": [
    {
      "label": "identity_attack",
      "results": [
        "{match: false, probabilities: Float32Array(2)}"
      ]
    },
    {
      "label": "insult",
      "results": [
        "{match: true, probabilities: Float32Array(2)}"
      ]
    },
    {
      "label": "obscene",
      "results": [
        "{match: null, probabilities: Float32Array(2)}"
      ]
    },
    {
      "label": "severe_toxicity",
      "results": [
        "{match: false, probabilities: Float32Array(2)}"
      ]
    },
    {
      "label": "sexual_explicit",
      "results": [
        "{match: null, probabilities: Float32Array(2)}"
      ]
    },
    {
      "label": "threat",
      "results": [
        "{match: false, probabilities: Float32Array(2)}"
      ]
    },
    {
      "label": "toxicity",
      "results": [
        "{match: true, probabilities: Float32Array(2)}"
      ]
    }
  ]
}
没有部落

此代码对我有效,仅排除有毒功能并使用您自己的。

import React from "react"

const toxic = () => {
    return {
  "object": [
    {
      "label": "identity_attack",
      "results": [
        "{match: false, probabilities: Float32Array(2)}"
      ]
    },
    {
      "label": "insult",
      "results": [
        "{match: true, probabilities: Float32Array(2)}"
      ]
    },
    {
      "label": "obscene",
      "results": [
        "{match: null, probabilities: Float32Array(2)}"
      ]
    },
    {
      "label": "severe_toxicity",
      "results": [
        "{match: false, probabilities: Float32Array(2)}"
      ]
    },
    {
      "label": "sexual_explicit",
      "results": [
        "{match: null, probabilities: Float32Array(2)}"
      ]
    },
    {
      "label": "threat",
      "results": [
        "{match: false, probabilities: Float32Array(2)}"
      ]
    },
    {
      "label": "toxicity",
      "results": [
        "{match: true, probabilities: Float32Array(2)}"
      ]
    }
  ]
}

}

function ToxicLabels(theObject) {
  return (
  <h2>{theObject.label}  match {theObject.results[0].match}</h2>
  )
}

export default class App extends React.Component {
  constructor() {
    super()
    this.state = {
      render: false, 
      objects: [],
    }
  }

  componentDidMount() {
    this.renderThePost()
  }
  componentDidUpdate(){
    this.render()
  }

  renderThePost = async () => {
    try {
      let response = await toxic('you suck')
      console.log( response )
      this.setState({
        objects: response.object,
        render: true
      })
      // this.state.object.map((object)=>{
      //   console.log(object)
      //   ToxicLabels(object)
      // })
      }catch (err) {
        console.log(err)
      }
    }

  render() {
      if( !this.state.render ) { return <div></div> }
      return (
        <div>
          <h2>Hello {"Hola"}</h2>
          {this.state.objects.map((object) => {
            
            return ToxicLabels(object)
            })}
        </div>
      )
    }
  }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

遍历对象并将其显示在表Angular 6中

如何遍历数组并将其显示在$ scope(angularjs)中

循环遍历对象数组并将其存储在表中[JS]

如何在Python中创建变量对象并将其返回?

如何在状态中遍历相似对象并将其推入对象?

如何在Vapor 1.5中遍历JSON对象并将其转换为[String:Any]?

如何循环遍历 jason 数据并将其显示在数据列表标签 (dl) 中

如何在一个地方(如 main)捕获异步异常并将其显示在 AlertDialog 中?

如何使用React在某些条件下遍历数组并将其推入单独的数组中?

如何使用d3js的数据方法解析对象数组并将其显示在工具提示中?

如何获取 JS 对象并将其传回?

循环遍历json对象并将其放在html表中

如何从文档中检索嵌套对象并将其显示在FirestoreRecyclerOptions中?

如何从React Native中的异步JS函数返回变量的值

在json对象中创建json对象并将其返回给Ajax

如何使用React.js在JSX中遍历对象

如何在react js中循环遍历json嵌套对象

如何在Redux选择器中创建新对象并将其返回

如何从数组中获取单个项目并将其显示为对象?而不是作为数组 Mongodb

我如何从json对象加载图像并将其显示在有角的ui网格中

遍历对象数组并显示项目[REACT JS]

如何遍历数组并将其值存储在Laravel中

如何从快速后端服务器获取图像并将其显示到 React js 前端?

如何在异步调用期间填充数组并将其发送到响应对象中

返回GET值并将其存储在JS中的变量中

如何从 API 中过滤特定数据并将其显示在 React Native Flatlist 中?

在React JS中遍历数组的对象

如何使用 React Hooks 获取对象数组并将其作为普通文本呈现在组件中?

如何使用 React 进行 API 调用并将其显示在表格中?