React第二天,今天写app开屏广告效果实例——终于知道 react native的强大

zhuanbike 2021-12-27 1042

使用了state生命周期,居然写成功……

直接上代码:实例地址 http://www.zhuanbike.com/react/01.html  2秒state生命周期

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>React开屏广告效果</title>
<script src="./js/react.development.js"></script>
<script src="./js/react-dom.development.js"></script>
<script src="./js/babel.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style tyle=text/css>
    #root img {
        width:100%;
    }
    #root{max-width:780px;margin:0 auto;}
</style>
</head>
<body>
 
<div id="root"></div>
<script type="text/babel">
const todoList = ["模块1", "模块2", "模块3", "模块4"];//模块加入可以替换成其他jsx
class Todo extends React.Component {
  render() {
    return <div>{this.props.content}</div>;
  }
}
class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      todoList: [<img src='./img/sk01.jpeg'></img>]
    };
  }
  componentDidMount() {
    this.timer = setTimeout(() => {
      this.setState({
        todoList: todoList
      });
    }, 2000);
  }
  componentWillUnmount() {
    clearTimeout(this.timer);
  }
  render() {
    return (
      <div>
        {this.state.todoList.map((todo, index) => (
          <Todo content={todo} key={index} />
        ))}
      </div>
    );
  }
}
ReactDOM.render(<App />, document.getElementById("root"));
</script>
 
</body>
</html>


最新回复 (2)
  • zhuanbike 2021-12-27
    0 引用 2
    初次使用react,还是手生,调试了很多遍才成功,这个可以做为标范本。
  • 然仔 2021-12-27
    0 引用 3
    加上演示地址吧,第一次使用react写组件 http://www.zhuanbike.com/react/01.html
发新帖