2 回答
TA贡献1752条经验 获得超4个赞
此代码适用于我,只需排除有毒功能并使用您自己的功能即可。
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>
)
}
}
TA贡献2021条经验 获得超8个赞
这可以解决。
this.setState({
objects: response.object,
})
...
{this.state.objects.map((object)=> ToxicLabels(object))}
添加回答
举报