React返回状态

huangapple go评论67阅读模式
英文:

React return state

问题

The line "why {this.state.number} return : "xxxx"" is asking why the content within {this.state.number} is displaying as "xxxx". It's displaying as "xxxx" because the content is being HTML-encoded, which is why you see " instead of double quotes.

To display the content as plain text, you can use the dangerouslySetInnerHTML attribute to render it as raw HTML. However, be cautious when using this approach as it can pose security risks if the content comes from an untrusted source.

Here's an example of how you can render the content without HTML encoding:

<div>
  <table className="Token-section-output" dangerouslySetInnerHTML={{ __html: this.state.number }}></table>
</div>

Please use this approach carefully, especially if the content you're rendering contains user-generated or untrusted data, as it can lead to cross-site scripting (XSS) vulnerabilities if not properly sanitized.

英文:
import React from &quot;react&quot;;
import { renderToString } from &#39;react-dom/server&#39;


export default class Tokens extends React.Component {
  state = {
    loading: true,
    number: null
  };


  async outDataTockens(val){
    val.forEach((ele,ind) =&gt;{
      console.log(ele);
    })
  }

  async componentDidMount() {
    let tokensvar = &#39;&lt;tr&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;2&lt;/td&gt; &lt;/tr&gt;&#39;;
    const url = &quot;https://api.multiversx.com/accounts/erd1n0kgesdn7hc63amdfygqfdj9enfa07n2hwup9y8732fg35qcug8q0w0x8s/tokens&quot;;
    const response = await fetch(url);
    const data = await response.json();
    data.forEach((ele,ind) =&gt;{
      tokensvar += &#39;&lt;tr&gt; &lt;td&gt;{ele.name}&lt;/td&gt; &lt;td&gt;{ele.name}&lt;/td&gt; &lt;/tr&gt;&#39;;
                  
      //&lt;tr&gt; &lt;td&gt;{ele.name}&lt;/td&gt; &lt;td&gt;{ele.name}&lt;/td&gt; &lt;/tr&gt;
      //tokensvar + &quot; &quot; + ele.name;
      //console.log(JSON.stringify(tokensvar));
      this.setState({ number: tokensvar, loading: false });
      //console.log(this.state.number);
    })
    //console.log(this.state);
  }

  

  render() {
    if (this.state.loading) {
      return &lt;div&gt;loading...&lt;/div&gt;;
    }

    if (!this.state.number) {
      return &lt;div&gt;didn&#39;t get number&lt;/div&gt;;
    }
    


    return (
      &lt;div&gt;
        &lt;table className=&quot;Token-section-output&quot;&gt; {this.state.number} &lt;/table&gt;
      &lt;/div&gt;
      
    );
  }
}

why {this.state.number} return : "xxxx"
And how to do for this return : xxxx

???

enter image description here

I dont know what i must do
I tried a lot of things but I don't know what to do I'm lost, but I still think it's a simple error and easy to solve aresolved for an experimenter, in any case thank you in advance for your help.

答案1

得分: 0

请将以下内容翻译成中文:

"Pls update the data into component's state as an array and then render using map fun.

here is an example

  • update API response
 const data = await response.json();
 this.setState({ rows: data });
  • render table's row
 render(){
   &lt;table className=&quot;Token-section-output&quot;&gt;
    &lt;p&gt;table row&lt;/p&gt;
    {rows.map((row) =&gt; (
      &lt;tr&gt;
        &lt;td&gt;{row?.name}&lt;/td&gt;
      &lt;/tr&gt;
     ))}
   &lt;/table&gt;
 }

if it is possible, we should use reactHook to manage the state of components and there are many benefits. smaller components and easy to control the state and so on."

英文:

Pls update the data into component's state as an array and then render using map fun.

here is an example

  • update API response
 const data = await response.json();
 this.setState({ rows: data });
  • render table's row
 render(){
   &lt;table className=&quot;Token-section-output&quot;&gt;
    &lt;p&gt;table row&lt;/p&gt;
    {rows.map((row) =&gt; (
      &lt;tr&gt;
        &lt;td&gt;{row?.name}&lt;/td&gt;
      &lt;/tr&gt;
     ))}
   &lt;/table&gt;
 }

if it is possible, we should use reactHook to manage the state of components and there are many benefits. smaller components and easy to control the state and so on.

huangapple
  • 本文由 发表于 2023年5月21日 06:04:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297521.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定