英文:
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 "react";
import { renderToString } from 'react-dom/server'
export default class Tokens extends React.Component {
state = {
loading: true,
number: null
};
async outDataTockens(val){
val.forEach((ele,ind) =>{
console.log(ele);
})
}
async componentDidMount() {
let tokensvar = '<tr> <td>1</td> <td>2</td> </tr>';
const url = "https://api.multiversx.com/accounts/erd1n0kgesdn7hc63amdfygqfdj9enfa07n2hwup9y8732fg35qcug8q0w0x8s/tokens";
const response = await fetch(url);
const data = await response.json();
data.forEach((ele,ind) =>{
tokensvar += '<tr> <td>{ele.name}</td> <td>{ele.name}</td> </tr>';
//<tr> <td>{ele.name}</td> <td>{ele.name}</td> </tr>
//tokensvar + " " + 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 <div>loading...</div>;
}
if (!this.state.number) {
return <div>didn't get number</div>;
}
return (
<div>
<table className="Token-section-output"> {this.state.number} </table>
</div>
);
}
}
why {this.state.number} return : "xxxx"
And how to do for this return : xxxx
???
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.
- update API response
const data = await response.json();
this.setState({ rows: data });
- render table's row
render(){
<table className="Token-section-output">
<p>table row</p>
{rows.map((row) => (
<tr>
<td>{row?.name}</td>
</tr>
))}
</table>
}
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.
- update API response
const data = await response.json();
this.setState({ rows: data });
- render table's row
render(){
<table className="Token-section-output">
<p>table row</p>
{rows.map((row) => (
<tr>
<td>{row?.name}</td>
</tr>
))}
</table>
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论