欢迎您 本站地址:
复制网址
手机查看
切换背景
源代码:
html编辑器
颜色值转换
点击运行
小库网 React 实例</title> <script src="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/react/18.2.0/umd/react.production.min.js" ></script> <script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/react-dom/18.2.0/umd/react-dom.production.min.js"></script> <script src="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/babel-standalone/6.26.0/babel.min.js" ></script> <div id="root"></div> <script type="text/babel"> class Header extends React.Component { constructor(props) { super(props); this.state = {favoritesite: "runoob"}; } componentDidMount() { setTimeout(() => { this.setState({favoritesite: "google"}) }, 1000) } getSnapshotBeforeUpdate(prevProps, prevState) { document.getElementById("div1").innerHTML = "在更新前喜欢的网站是:" + prevState.favoritesite; } componentDidUpdate() { document.getElementById("div2").innerHTML = "更新后喜欢的网站是:" + this.state.favoritesite; } render() { return ( <div> <h1>我喜欢的网站是 {this.state.favoritesite}</h1> <div id="div1"></div> <div id="div2"></div> </div> ); } } const root = ReactDOM.createRoot(document.getElementById("root")); root.render(<Header />); </script>
运行结果:
小库提示
扫描下方二维码,访问手机版。
打赏
关闭