首页 > 其他分享 >react基础

react基础

时间:2022-10-12 23:01:58浏览次数:47  
标签:count return 基础 Component react state 组件

 

1.jsx语法

// 1.定义虚拟dom,不要写引号
// 2.标签中混入js表达式要用{}
 const myId="title"
     const myData="HELLO REACT"
    //1.创建虚拟dom
    const VDOM=(
      <h2>
        <span id={myId}>
          {myData}
          </span>
        </h2>
    )

 

// 3.样式的类名指定不要用class,要用className
 // 4.内联样式,要用style={{key:value}}的形式去写

<style> .title{ background-color: red; } </style> const VDOM=( <div> <h1 className="title" id="{id}"> <span style={{fontSize:'30px'}} >HELLO REACT</span> </h1> </div> )
 // 5.虚拟dom 必须只有一个根标签
 //6.标签必须闭合
    const VDOM=(
       <div>
          <h1 className="title" id="{id}">
          <span style={{fontSize:'30px'}} >HELLO REACT</span>
          </h1>
           <input type="password" name="" id="" >  // 报错
          </div>
          <div></div> // 会报错
      )
 // 7.标签首字母:
     // 若为小写,会将该标签转化为html同名元素,若HTML没有对应的同名元素,会报错
     //若为大写,会渲染对应的组件
    
  <div>
          <h1 className="title" >
          <span style={{fontSize:'30px'}} >HELLO REACT</span>
          </h1>
    <good>123</good> //会报错
   <Good></Good>
          </div>

 

  // 8 数组循环渲染用map
const data=["vue","react","anguler"]
    //1.创建虚拟dom
    const VDOM=(
       <div>
         <ul>
          {data.map((item,index)=>{
          return  <li key={index}>{item}</li>
          })}
          </ul>
          </div>)

 

2.全局安装react脚手架及创建项目

  npm install -g create-react-app

  create-react-app + 项目名字

 

 

3.函数是组件

//1 首字母必须大写
function Demo () {   return (     <div>       <h2>函数式组件</h2>       <span>123123</span>     </div>   ) } export default Demo
//react解析组件标签,找到Demo组件
  // 发现组件是使用函数定义,随后调用该函数,将返回的虚拟dom转化成真是dom,随后渲染到页面上

4.类似组件

import React from "react"; class MyCompnent extends React.Component {   render () {//放在MyCompnent的原型对象上供实列使用 render中的this指向MyCompnent组件实列对象     return <div>hello react       <h2> react</h2>     </div>   } } export default MyCompnent   //react解析组件标签,找到MyCompnent组件   // 发现组件是使用类定义,随后new出该类的实列,并通过该实例调用原型上的render方法   //将render返回的虚拟dom转化成真是dom,随后渲染到页面上

 5.组件样式

import React, { Component } from "react";
import "../css/index.css"

export default class App extends Component {
  render () {
    let obj = { //驼峰命名
      backgroundColor: "red",
      fontSize: "30px"
    }
    return (
      <div style={obj}>
        hello React
        <div className="active">sdfsdf</div>
      </div>
    )
  }
}

6.事件绑定

import React, { Component } from "react";
export default class App extends Component {
  a = 444
  render () {
    return <div>
      <button onClick={this.handleClick}>add</button>
      <button onClick={() => this.handleClick()}>add1</button>
    </div>
  }
  handleClick = () => {
    console.log(this.a);
  }
  // 需要传参必须使用() => this.handleClick()方式
}

 7.ref的使用

import React, { Component } from "react";
export default class App extends Component {
  a = 444
  myRef = React.createRef() //返回一个ref对象
  render () {
    return <div>
      <input ref={this.myRef} />
      <button onClick={() => this.handleClick()}>add1</button>
    </div>
  }
  handleClick = () => {
    console.log(this.a);
    // console.log(this.refs.myText.value);
    console.log(this.myRef.current.value);
  }
}

8.状态(state)的使用

import React, { Component } from "react"; export default class App extends Component {   state = {     isShow: true,     count: 1   }   render () {     return <div>       <button onClick={() => {         this.handleClick()       }}>{this.state.isShow ? "收藏" : "取消收藏"}</button>       <div>{this.state.count}</div><button onClick={this.handleAdd1}>add1</button>
    </div>   }   handleClick = () => {     const isShow = this.state.isShow     this.setState({       isShow: !isShow     })     console.log('被输出值{  }的输出结果是:', this);   }   handleAdd1 = () => {     this.setState({   //setState是异步更新的       count: this.state.count + 1     }, () => {       console.log(this.state.count);//2     })
    console.log(this.state.count);//1
  }
}

9.简单的todolist案例

import React, { Component } from "react";
export default class App extends Component {

  myRef = React.createRef() //返回一个ref对象

  state = {
    list: ["wqeqwe", "aaa", "dddd"]
  }
  render () {
    return <div style={{ margin: "50px 200px" }}>
      <input ref={this.myRef} />
      <button onClick={() => this.handleAdd()}>add</button>

      <ul>
        {this.state.list.map((item, index) => {
          return <li onClick={() => this.handleDel(index)} key={index}>{item}</li>
        })}
      </ul>
      {/* {条件渲染} */}
      {this.state.list.length === 0 ? <div>暂无数据</div> : null}
    </div>
  }
  /**
   * 添加
   */
  handleAdd = () => {
    const newList = [...this.state.list]
    newList.push(this.myRef.current.value)
    this.setState({
      list: newList
    })
    this.myRef.current.value = ""
  }
  /**
   * 删除
   */
  handleDel = (index) => {
    const newList = [...this.state.list]
    newList.splice(index, 1)
    this.setState({
      list: newList
    })
  }
}

 10.属性(props)之父子组件通讯

//父组件
import React, { Component } from "react"; import NavBar from "../component/nav-bar"; import SideBar from "../component/side-bar"; export default class App extends Component {   state = {     count: 1   }   myRef = React.createRef()   render () {     return <div>       {/* <h2>首页</h2> */}
      <NavBar ref={this.myRef} title="首页" event={(data) => this.handleEvent(data)} count={this.state.count} isShow={false}></NavBar>       <button onClick={() => {         console.log('被输出值{  }的输出结果是:', this.myRef);         //通过ref修改子组件的状态         this.myRef.current.setState({           num: this.myRef.current.state.num + 1         })       }}>修改子组件num</button>       <SideBar bg="red"></SideBar>     </div>   }   //字传父回调   handleEvent = (data) => {     console.log('被输出值{  }的输出结果是:', data);     this.setState({       count: this.state.count + data     })   }
}
//子组件类式组件
import React, { Component } from "react"; import types from "prop-types" export default class NavBar extends Component {
  state = {     num: 2   }   //属性验证   static propTypes = { //类属性,通过类直接访问     title: types.string,     count: types.number,     isShow: types.bool   }   //属性设置默认值   static defaultProps = {     title: "",     count: 0,     isShow: false   }
  render () {     const { title, count, isShow } = this.props     return <div style={{ display: 'flex', margin: "20px" }}>       <div> {title}</div>       <div style={{ margin: "0 20px" }}>{count}</div>       {isShow ? <button >显示与隐藏</button> : null}       <button onClick={() => {         //不能直接修改props         // this.props.count = 2         //字传父         this.props.event(2)       }}>修改父组件的count</button>       <div>{this.state.num}</div>
      {/* { 属性是只读的,不能修改} */}     </div>   } }

//子组件函数是组件
import React from 'react' import types from "prop-types" function SideBar (props) {   console.log('被输出值{  }的输出结果是:', props);   const { bg } = props   return (     <div style={{ background: bg, width: "150px" }}>       <ul>         <li>1233</li>         <li>1233</li>         <li>1233</li>         <li>1233</li>         <li>1233</li>         <li>1233</li>         <li>1233</li>         <li>1233</li>         <li>1233</li>         <li>1233</li>       </ul>     </div>   ) }
export default SideBar //属性验证 SideBar.propTypes = {   bg: types.string } //默认值 SideBar.defaultProps = {   bg: "pink" }

 11.非父子组件通信发布订阅者模式

/* eslint-disable no-useless-constructor */
/* eslint-disable import/first */
//简单的发布订阅者模式
let bus = {
  list: [],
  //先订阅
  serscribe (callback) {
    this.list.push(callback)
  },

  // 再发布
  publish (data) {
    this.list.forEach(callback => {
      callback && callback(data)
    })
  }
}




import React, { Component } from 'react'

// 父组件
export default class publish extends Component {
  render () {
    return (
      <div>
        <Children1 />
        <Children2 />
      </div>
    )
  }
}

// 子组件1订阅
class Children1 extends Component {
  state = {
    count: 0
  }
  //生命周期挂载后
  componentDidMount () {
    bus.serscribe((data) => {
      this.setState({
        count: this.state.count + data
      })
    })
  }

  render () {
    return (
      <div>{this.state.count}</div>
    )
  }
}


// 子组件2发布
class Children2 extends Component {
  state = {
    num: 1
  }
  render () {
    return (
      <div >
        <button onClick={() => {
          bus.publish(this.state.num)
        }}>发布</button>
      </div>
    )
  }
}

12.祖孙组件通信context

import React, { Component } from 'react'
const GlobleContext = React.createContext()
// 父组件
export default class context extends Component {
  state = {
    num: 0
  }
  render () {
    return (
      <GlobleContext.Provider value={{
        son: "儿子",
        child: "孙子",
        change: (data) => {
          this.setState({
            num: this.state.num + data
          })
        }

      }}>
        <div>
          {this.state.num}
          <ContextSon />
        </div>
      </GlobleContext.Provider>
    )
  }
}



// 子组件
class ContextSon extends Component {
  render () {
    return (
      <GlobleContext.Consumer>
        {(value) => {

          return <div>
            {value.son}
            <ContextChild />
          </div>
        }}
      </GlobleContext.Consumer>
    )
  }
}

// 孙组件
class ContextChild extends Component {
  render () {
    return (
      <GlobleContext.Consumer>
        {(value) => {
          console.log('被输出值{  }的输出结果是:', value);
          return <div>{value.child}

            <button onClick={() => {
              value.change(1)
            }}>孙组件修改父组件状态</button>
          </div>
        }}
      </GlobleContext.Consumer>
    )
  }
}

 

标签:count,return,基础,Component,react,state,组件
From: https://www.cnblogs.com/chenxian123m/p/qweqe_232344_chc.html

相关文章

  • 计算机基础
    目录目录目录进制转换命令行进制转换命令行改变目录 cd显示一个目录下的子目录和文件 dir新建目录 md新建文件 cd>test.txt编辑文件 echo内容>test.txt复制文......
  • 部分Java基础进阶内容 - 随笔
    JAVA进阶对象序列化对象要序列化要实现Serializable接口然后通过ObjectInputStream对象读入流来读入一个对象newObjectOutputStream(newxxOutputStream(""))new的......
  • java语言基础
    初识计算机和java语言1.计算机由硬件和软件组成计算机中的主要硬件cpu是计算机中最核心的部件,类似人的大脑一台计算机的运算核心和控制核心都由cpu完成其主要功能是......
  • Linux基础
    1.目录结构介绍基本介绍LIunx的文件系统是采用层式的树状目录结构,在此结构中的是最上层是根目录/,然后在次目录下在创建其他目录。在Liunx世界里,一切皆为文件树状目......
  • python基础-较复杂数据类型预览
    1.初识列表  列表就是队列;  列表是一种有序的,且内容可重复的数据类型;  用list代表列表,也可以用list()定义一个列表,同时定义列表可以直接使用[];  python中列......
  • python基础之多层语法糖、函数递归
    python基础之多层语法糖、函数递归目录一、多层语法糖二、装饰器模版三、装饰器修复技术四、函数的递归调用1.函数的递归调用2.递归函数的定义3.递归函数的特点4.递归函数......
  • React:环境安装
    环境安装可以使用node-v查看node.js版本号。$npmconfigsetregistryhttps://r.npm.taobao.org//配过镜像的可以不用再配,也可以不配置直接用npm$cnpminstall-......
  • 【杂谈】爬虫基础与快速入门指南
    今天给大家分享一下网络爬虫的基础知识,以及一些优秀的开源爬虫项目。网络爬虫主要是我们在面对新的任务,但自己又没有数据的时候,获取自己想要的数据的一种手段。因此我们有必......
  • React Cocurrent 并发性
    其根本原因就是JavaScript是单线程的,那么和react提出Cocurrent类似的概念就是Promise异步?我们并不能同时做两件事而是一件事开始后先onhold然后处理另外一件事最后返回......
  • Java基础语法 数组的默认初始化值
    默认初始化值packagecom.ljg.java;/**⑤数组元素的默认初始化值* >数组元素是整型:0* >数组元素是浮点型:0.0* >数组元素是char型:0或'\u0000',而非'0'......