Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何将字符串转换为纯html?_Reactjs - Fatal编程技术网

Reactjs 如何将字符串转换为纯html?

Reactjs 如何将字符串转换为纯html?,reactjs,Reactjs,不确定正确的词汇,所以我很难用谷歌搜索。基本上我发送一个get请求 它将返回我向下滚动的选项标签。基本上使用axios componentDidMount() { axios.get("http://localhost:/8080/api/v1/getstuff" ) .then((response) => { this.setState({ stuff: response.data }) }) .catch(f

不确定正确的词汇,所以我很难用谷歌搜索。基本上我发送一个get请求

它将返回我向下滚动的选项标签。基本上使用axios

  componentDidMount() {

    axios.get("http://localhost:/8080/api/v1/getstuff"
    )
      .then((response) => {
        this.setState({ stuff: response.data })

      })
      .catch(function (error) {
        console.log(error);

      });

  }
stuff现在设置为:

我把它放在一个select标记中,但它显示为

"<option value='hello'>hello</option>
<option value='hello2'>hello2</option>
<option value='sss'>sss</option>"
“你好
你好
sss“
而不是

<option value='hello'>hello</option>
<option value='hello2'>hello2</option>
<option value='sss'>sss</option>
你好 你好 sss
这样选项标签就不会出现。如何修复?

您可以采取两种方法:

第一种方法是设置父select标记的内部html。大概是这样的:

<select dangerouslySetInnerHTML={this.state.stuff}></select>
[{
    "value": "hello",
    "text": "hello"
},
{
    "value": "hello2",
    "text": "hello2"
},
{
    "value": "sss",
    "text": "sss"
}]
<select>
    {this.state.stuff.map((o,i) => <option key={i} value={o.value}>{o.text}</option>)}
</select>
然后,你可以这样做:

<select dangerouslySetInnerHTML={this.state.stuff}></select>
[{
    "value": "hello",
    "text": "hello"
},
{
    "value": "hello2",
    "text": "hello2"
},
{
    "value": "sss",
    "text": "sss"
}]
<select>
    {this.state.stuff.map((o,i) => <option key={i} value={o.value}>{o.text}</option>)}
</select>

{this.state.stuff.map((o,i)=>{o.text}