欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

react组件基本用法示例小结

程序员文章站 2022-12-06 23:04:33
本文实例讲述了react组件基本用法。分享给大家供大家参考,具体如下:组件间传值: &l...

本文实例讲述了react组件基本用法。分享给大家供大家参考,具体如下:

组件间传值:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>test</title>
</head>
<body>
<div id="app">
 
</div>
<script src="../js/react.production.min.js"></script><!--react核心库-->
<script src="../js/react-dom.production.min.js"></script><!--操作dom的react扩展库-->
<script src="../js/babel.min.js"></script><!--解析jsx语法-->
<script type="text/babel">
  class com extends react.component {
    click = ()=>{
      // console.log(this.input);
      // console.log(this.p.innertext);
      console.log(this.refs.my.value);//父组件访问子组件用refs
    };
 
    render() {
      return (
        <div>
          <input type="text" ref={(input)=>{this.input=input}}/>
          <p ref={(p)=>{this.p=p}}>我是段落</p>
          <input type="text" ref="my"/>
          <button onclick={this.click}>点击</button>
        </div>
      );
    }
  }
 
  reactdom.render(<com/>,document.getelementbyid('app'));
</script>
</body>
</html>

列表:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>test</title>
</head>
<body>
<div id="app"></div>
<script src="../js/react.production.min.js"></script><!--react核心库-->
<script src="../js/react-dom.production.min.js"></script><!--操作dom的react扩展库-->
<script src="../js/babel.min.js"></script><!--解析jsx语法-->
<script type="text/babel">
  class add extends react.component {
    click = ()=>{
      this.props.alladd(this.input.value);
    };
    render() {
      const {length} = this.props;
      return (
        <div>
          <input type="text" ref={(input)=>{this.input=input}}/>
          <button onclick={this.click}>add{length}</button>
        </div>
      )
    }
  }
 
  class list extends react.component {
    render() {
      const {list} = this.props;
      return (
        <div>
          <ul>
            {
              list.map((v,i)=>{
                return <li key={i}>{v}</li>
              })
            }
          </ul>
        </div>
      )
    }
  }
 
  class app extends react.component {
    state = {
      list:['吃饭','睡觉','打游戏','游泳']
    };
    add = (value)=>{
      const {list} = this.state;//获取原先的list
      list.unshift(value);//将添加的值传入list
      this.setstate(list);//重新设置list
    };
    render() {
      const {list} = this.state;//获取list
      return (
        <div>
          <add alladd={this.add} length={list.length}/>
          <list list={list} />
        </div>
      )
    }
  }
 
  reactdom.render(<app />,document.getelementbyid('app'));
</script>
</body>
</html>

受控组件和非受控组件:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>test</title>
</head>
<body>
<div id="app"></div>
<script src="../js/react.production.min.js"></script><!--react核心库-->
<script src="../js/react-dom.production.min.js"></script><!--操作dom的react扩展库-->
<script src="../js/babel.min.js"></script><!--解析jsx语法-->
<script type="text/babel">
  class com extends react.component {
    state = {
      age:''
    };
    //非受控组件 不受state控制
    click = () => {
      console.log(this.input.value);
    };
    change = (event)=>{
      console.log(event.target.value);
      this.setstate({
        age: event.target.value
      })
    };
    render() {
      const {age} = this.props;
      return (
        <div>
          姓名:<input type="text" ref={(input)=>{this.input=input}}/>
          <button onclick={this.click}>获取姓名</button>
          年龄:<input type="text" value={age} onchange={this.change}/>
        </div>
      );
    };
  }
  reactdom.render(<com />,document.getelementbyid('app'));
</script>
</body>
</html>

组件生命周期:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>test</title>
</head>
<body>
<div id="app"></div>
<script src="../js/react.production.min.js"></script><!--react核心库-->
<script src="../js/react-dom.production.min.js"></script><!--操作dom的react扩展库-->
<script src="../js/babel.min.js"></script><!--解析jsx语法-->
<script type="text/babel">
  class com extends react.component {
    state = {
      msg:123
    };
 
    sing() {
      return new promise((resolve,reject)=>{
        settimeout(()=>{
          resolve('唱一首歌');
        },1000);
      })
    };
 
    async get() {
      await this.sing().then((res)=>{
        console.log(res);
      });
    };
 
    componentwillmount() {
      //will会先执行,但不一定先执行完毕
      console.log('之前');
      // this.get();
    }
 
    componentdidmount() {
      //进行ajax操作,获取后台数据
      console.log('之后');
    }
 
    shouldcomponentupdate(nextprops, nextstate) {
      const {msg} = this.state;
 
      //如果没有
      if ({msg} !== nextstate) {
        return true;
      }
      console.log('更新');
      return false;
    }
 
    componentwillupdate() {
      console.log('更新之前');
    }
 
    componentdidupdate() {
      console.log('更新之后');
      //再次获取数据
    }
 
    click = ()=>{
      this.setstate({
        msg: 234
      })
    };
 
    render() {
      console.log('render');
      const {msg} = this.state;
      return(
        <div>
          <h1>{msg}</h1>
          <button onclick={this.click}>更新</button>
        </div>
      )
    }
  }
 
  reactdom.render(<com />,document.getelementbyid('app'));
</script>
</body>
</html>

希望本文所述对大家react程序设计有所帮助。