React Native

React Native 跨页面通信、发送通知

2021-09-02  本文已影响0人  _小爽_

1、发送页面A

import { View, DeviceEventEmitter } from 'react-native'
export default function A({ props}) {
    //发送通知
    const sendMsg = () => {
        console.log("发送通知 send")
        let send = {
            name:"test",
            age:18
        }
        DeviceEventEmitter.emit("sendmsg",send)
    }
    ...
}

2、接收、监听页面B

import React, { useEffect} from 'react'
import { View, DeviceEventEmitter } from 'react-native'

export default function B({ props}) {
    let sub = null
    useEffect(() => {
        console.log("加载监听")
        sub = DeviceEventEmitter.addListener("sendmsg",res=>{
            console.log("接收到send = "+JSON.stringify(res))
        })
        return () => {
            console.log("移除send")
            sub.remove()
        }
    }, [])
    ...
 }
send.png
上一篇 下一篇

猜你喜欢

热点阅读