我有一個字符串,它實際上是一個嵌套的反應組件名稱及其道具。想要在另一個反應組件內渲染該字符串。 例如在React組件中轉換字符串
var String = "<parentComponent title='Parent'><childComponent age='23'></childComponent></parentComponent>"
陣營代碼: -
class MainComponnet extends Component{
render(){
let stringComponents = "<parentComponent title='Parent'><childComponent age='23'></childComponent></parentComponent>";
return(
{stringComponents}
)
}
}
父組件JSX: -
class ParentComponent extends Component{
render(){
return(
<div> {this.props.title}</div>
)
}
}
輔元件JSX: -
class ChildComponent extends Component{
render(){
return(
<div> {this.props.age}</div>
)
}
}
請幫助..
一個問題,爲什麼它在一個字符串內? – VivekN
其實服務將返回組件字符串,因此我必須呈現該組件。 – Kracki
我認爲這樣做有些不可能。可能你需要一個react-jsx-parser來將字符串轉換爲正常的組件類型,然後渲染它 – VivekN