react的通信方式

# 简介(6种)

  • props,父子通信
  • 回调函数,子父通信
  • 变量提升,兄弟组建通信
  • Context,跨组建通信
  • node的events模块的单例通信
  • redux共享数据通信

# 方式

# props[父子通信]

function Children(props) {
    return (
        <div>
        <p>Children</p>
        <p>{props.text}</p>
        </div>
    )
}
function Parent() {
    return (
        <div>
        <p>Parent</p>
        <Children text="这是爸爸传给你的东西"></Children>
        </div>
    )
}
export default Parent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 回调函数[子父通信]

function Children(props) {
  return (
    <div>
      <p>Children</p>
      <p>{props.text}</p>
      <button onClick={() => { props.handleChange('改变了') }}>
        点击我改变爸爸传给我的东西
      </button>
    </div>
  )
}

function Parent() {
  let [text, setText] = useState('这是爸爸传给你的东西')
  function handleChange(val) {
    setText(val)
  }
  return (
    <div>
      <p>Parent</p>
      <Children handleChange={handleChange} text={text}></Children>
    </div>
  )
}
export default Parent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

# 变量提升[兄弟组建通信]

function Children(props) {
  return (
    <div>
      <p>Children</p>
      <button onClick={() => { props.setText('我是Children发的信息') }}>给Children1发信息</button>
    </div>
  )
}
function Children1(props) {
  return (
    <div>
      <p>Children1</p>
      <p>{props.text}</p>
    </div>
  )
}
function App() {
  let [text, setText] = useState('')
  return (
    <>
      <div>APP</div>
      <Children setText={setText}></Children>
      <Children1 text={text}></Children1>
    </>
  )
}
export default App
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

# Context[跨组建通信]

# 旧写法

class Children extends React.Component {
  static contextTypes = {
    text: PropsType.string
  }
  render() {
    return (
      <div>
        <p>Children</p>
        <p>{this.context.text}</p>
      </div>
    )
  }
}

class Parent extends React.Component {
  static childContextTypes = {
    text: PropsType.string
  }
  getChildContext() {
    return {
      text: '我是爸爸的信息'
    }
  }
  render() {
    return (
        <div>
          <p>Parent</p>
          <Children></Children>
        </div>
    )
  }
}
export default Parent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

# 新写法

const { Consumer, Provider } = React.createContext()

class Children extends React.Component {
  render() {
    return (
      <Consumer>
        {
          (value) => (
            <div>
              <p>Children1</p>
              <p>{value.text}</p>
            </div>
          )
        }
      </Consumer>
    )
  }
}

class Parent extends React.Component {
  render() {
    return (
      <Provider value={{ text: '我是context' }}>
        <div>
          <p>Parent</p>
          <Children1></Children1>
        </div>
      </Provider>
    )
  }
}

export default Parent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

# node的events模块[单例通信]

注意⚠️:这种通信记住在顶部引入events模块,无需安装,node自身模块。如果是web端的话,要引入event库;

function Children(props) {
  return (
    <div>
      <p>Children</p>
      <p>{props.text}</p>
      <button onClick={() => { props.event.emit('foo') }}>点击我改变爸爸传给我的东西</button>
    </div>
  )
}

function Parent() {
  let [text, setText] = useState('这是爸爸传给你的东西')
  let event = new Events()
  event.on('foo', () => { setText('改变了') })
  return (
    <div>
      <p>Parent</p>
      <Children event={event} text={text}></Children>
    </div>
  )
}
export default Parent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# redux/dva共享数据通信

注意⚠:记得安装reduxreact-redux

store.js

import { createStore } from 'redux'
let defaultState = {
    text: '我是store'
}
let reducer = (state = defaultState, action) => {
    switch (action) {
        default: return state
    }
}
export default createStore(reducer)
1
2
3
4
5
6
7
8
9
10

child.js

import React from 'react'
import { connect } from 'react-redux'

class Child extends React.Component {
    render() {
        return (
            <div>Child<p>{this.props.text}</p></div>
        )
    }
}
let mapStataToProps = (state) => {
    return {
        text: state.text
    }
}
export default connect(mapStataToProps, null)(Child)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Parent.js

class Parent extends React.Component {
  render() {
    return (
      <Provider store={store}>
        <div>
          <p>Parent</p>
          <Child></Child>
        </div>
      </Provider>
    )
  }
}
export default Parent
1
2
3
4
5
6
7
8
9
10
11
12
13
上次更新: 2022/04/15, 05:41:28
×