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