gpt4 book ai didi

reactjs - React JSX 初始化后附加 HTML(react 对象)

转载 作者:行者123 更新时间:2023-12-03 13:28:21 25 4
gpt4 key购买 nike

如何使这个函数工作,在初始化和返回之间附加到我的 react 对象

render () {
var users = this.state.users,
appContainer = <div id="container"></div>

for (var i = 0; i < users.length; i++) {
// this doesn't work
appContainer.appendChild(this.createCard(users[i]));
}

return appContainer;
}

最佳答案

抱歉误读了问题。您想要做的事情可以通过 Ryan Florence 和 Michael Jackson here 描述的“门户”模式来实现。 。以下是解决我认为您的问题的尝试。如果我仍然不明白你的问题,请原谅我。

render() {
// dont render anything, open a "portal"
return <div ref="container" />;
}

componentDidMount() {
// get initialized component markup
var container = this.refs.container;
var cards = [];

for (var i = 0; i < users.length; i++) {
cards.push(this.createCard(users[i]));
}

// start a new React render tree with the container and the cards
// passed in from above, this is the other side of the portal.
ReactDOM.render(<div>{cards}</div>, container):
}

关于reactjs - React JSX 初始化后附加 HTML(react 对象),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38192552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com