03react基础-组件1

2021-05-09  本文已影响0人  东邪_黄药师

react组件的创建方式

一.函数创建组件

import React from 'react'
import ReactDOM from 'react-dom'

function Hello() {
  return (
    <div>这是我的第一个函数组件</div>
  )
}

利用ReactDOM.render()进行渲染

ReactDOM.render(<Hello />, document.getElementById('root'))

②使用箭头函数创建组件

import React from 'react'
import ReactDOM from 'react-dom'
// 使用箭头函数创建组件:
const Hello = () => <div>这是我的第一个函数组件</div>
// 渲染组件
ReactDOM.render(<Hello />, document.getElementById('root'))

二.使用类创建组件

import React from 'react'
import ReactDOM from 'react-dom'
// 创建类组件
class Hi extends React.Component {
  render () {
 // 创建class类,继承React.Component,在里面提供render方法,在return里面返回内容
    return (
      <h1>1234</h1>
    )
  }
}
// 渲染组件
ReactDOM.render(<Hi />, document.getElementById('root'))

抽离成单独的JS文件

示例demo
import React from 'react'
// 1.创建组件
class Hello extends React.Component {
  render() {
    return (
      <div>这是我的第一个抽离到js文件中的组件</div>
    )
  }
}
// 2.导出组件
export default Hello

2.在index.js中导入Hello组件,渲染到页面

import Hello from './js/Hello'
ReactDOM.render(<Hello />,document.getElementById('root'))

事件

事件绑定

import React from 'react'
import ReactDOM from 'react-dom'
class App extends React.Component {
  // 事件处理程序
  handleClick() {
    console.log('单击事件触发了')
  }
  render() {
    return (
      <button onClick={this.handleClick}>点我,点我</button>
    )
  }
}
// 渲染组件
ReactDOM.render(<App />,  document.getElementById('root'))

demo2:通过函数组件绑定事件:

import React from 'react'
import ReactDOM from 'react-dom'
// 通过函数组件绑定事件:
function App() {
  // 事件处理程序
  function handleClick() {
    console.log('函数组件中的事件绑定,事件触发了')
  }

  return (
    <button onClick={handleClick}>点我</button>
  )
}
// 渲染组件
ReactDOM.render(<App />,  document.getElementById('root'))

事件对象

import React from 'react'
import ReactDOM from 'react-dom'
class App extends React.Component {
  handleClick(e) {
    // 阻止浏览器的默认行为
    e.preventDefault()
    console.log('a标签的单击事件触发了')
  }
  render() {
    return (
      <a href="http://baidu.com/" onClick={this.handleClick}>打开百度</a>
    )
  }
}
// 渲染组件
ReactDOM.render(<App />,  document.getElementById('root'))

有状态组件和无状态组件

State和SetState

state基本使用

export default class extends React.Component {
    constructor(){
        super()

        // 第一种初始化方式
        this.state = {
            count : 0
        }
    }
    // 第二种初始化方式
    state = {
        count:1
    }
    render(){
        return (
            <div>计数器 :{this.state.count}</div>
        )
    }
}

setState() 修改状态

import React from 'react'
import ReactDOM from 'react-dom'

/* 
  state的基本使用
*/

class App extends React.Component {
  state = {
    count: 0,
    test: 0
  }

  render() {
    return (
      <div>
        <h1>计数器:{ this.state.count }</h1>
        <h1>计数器:{ this.state.test }</h1>
        <button onClick={() => {
          this.setState({
            count: this.state.count +=3,
            test:this.state.test -=3
          })

          // 错误!!!
          // this.state.count += 1
        }}>+1</button>
      </div>
    )
  }
}

// 渲染组件
ReactDOM.render(<App />,  document.getElementById('root'))
image.png

抽取事件处理函数

当我们把上面代码的事件处理程序抽取出来后,会报错,找不到this


image.png

在JSX中我们写的事件处理函数可以找到this,原因在于在JSX中我们利用箭头函数,箭头函数是不会绑定this,所以会向外一层去寻找,外层是render方法,在render方法里面的this刚好指向的是当前实例对象

事件绑定this指向(3种)

1.箭头函数
2.利用bind方法

利用原型bind方法是可以更改函数里面this的指向的,所以我们可以在构造中调用bind方法,然后把返回的值赋值给我们的函数即可

import React from 'react'
import ReactDOM from 'react-dom'

/* 
  从JSX中抽离事件处理程序
*/

class App extends React.Component {
  constructor() {
    super()

    this.state = {
      count: 0
    }

    this.onIncrement = this.onIncrement.bind(this)
  }
  // 事件处理程序
  onIncrement() {
    console.log('事件处理程序中的this:', this)
    this.setState({
      count: this.state.count + 1
    })
  }
 render() {
    return (
      <div>
        <h1>计数器:{ this.state.count }</h1>
        <button onClick={this.onIncrement}>+1</button>
      </div>
    )
  }
}
// 渲染组件
ReactDOM.render(<App />,  document.getElementById('root'))
3.class的实例方法
import React from 'react'
import ReactDOM from 'react-dom'

/* 
  从JSX中抽离事件处理程序
*/

class App extends React.Component {
  state = {
    count: 0
  }

  // 事件处理程序
  onIncrement = () => {
    console.log('事件处理程序中的this:', this)
    this.setState({
      count: this.state.count + 1
    })
  }

  render() {
    return (
      <div>
        <h1>计数器:{ this.state.count }</h1>
        <button onClick={this.onIncrement}>+1</button>
      </div>
    )
  }
}

// 渲染组件
ReactDOM.render(<App />,  document.getElementById('root'))
使用class的实例方法,也是依赖箭头函数不绑定this的原因
上一篇 下一篇

猜你喜欢

热点阅读