当前位置: 首页 > news >正文

React Native + Redux实现一个公共消息组件

一、安装依赖

npm i @reduxjs/toolkit  react-redux 

二、创建store与slice

import {createSlice} from '@reduxjs/toolkit'const messageSlice = createSlice({name: 'message',initialState: {isShow: false,message: '',type: ''},reducers: {showMessage: (state,action) => {const { type, message } = action.payload;state.isShow = truestate.message = messagestate.type = type},hideMessage: (state) => {state.isShow = false}}
})export const { showMessage,hideMessage } = messageSlice.actions
export default messageSlice.reducer
import {configureStore} from '@reduxjs/toolkit'
import messageSlice from "@/store/slices/messageSlice";const store = configureStore({reducer: {message: messageSlice}
})export default store

三、自定义消息组件SimpleToast

import React, {useEffect, useRef} from 'react';
import {View,Text,StyleSheet
} from 'react-native';
import {useDispatch, useSelector} from 'react-redux'
import {hideMessage} from '@/store/slices/messageSlice'const SimpleToast = () => {const duration = 1500;const {isShow, message, type} = useSelector((state) => state.message);const timerRef = useRef(null);const dispatch = useDispatch();const getBackgroundColor = () => {const colors = {info: '#1890ff',success: '#52c41a',warning: '#fa8c16',error: '#ff4d4f'};return colors[type] || colors.info;};useEffect(() => {if (isShow) {// 清空之前的定时器if (timerRef.current) {clearTimeout(timerRef.current);}// 自动关闭timerRef.current = setTimeout(() => {dispatch(hideMessage());}, duration);}// 组件卸载时清理return () => {if (timerRef.current) {clearTimeout(timerRef.current);}};}, [isShow]);if (!isShow) {return null;}return (<Viewstyle={[styles.container,{backgroundColor: getBackgroundColor(),}]}pointerEvents="none" // 防止 toast 遮挡下方元素><Text style={styles.message} numberOfLines={2}>{message}</Text></View>);
};const styles = StyleSheet.create({container: {position: 'absolute',alignSelf: 'center', // 水平居中top: 100, // 距离顶部100pxzIndex: 9999,paddingHorizontal: 16,paddingVertical: 12,borderRadius: 8,maxWidth: '80%', // 最大宽度为屏幕宽度的80%},message: {color: 'white', // 文字为白色fontSize: 14,fontWeight: '500',textAlign: 'center',lineHeight: 20,// 不设置固定宽度,让文字自适应}
});export default SimpleToast;

四、放入app目录下的配置_layout.js中

import {Slot} from 'expo-router';
import '@/global.css'
import {Provider} from 'react-redux';
import store from "@/store";
import AuthInitializer from "@/components/AuthInitializer";
import SimpleToast from "@/components/SimpleToast";export default function Layout() {return <Provider store={store}><AuthInitializer><SimpleToast/><Slot/></AuthInitializer></Provider>
}
http://www.jsqmd.com/news/298458/

相关文章:

  • 2026学历提升优选:口碑学校开启智慧之门,国家开放大学招生/成人学历提升/学历提升/专升本报名,学历提升学校哪个好
  • 当AI成为“决策代理“,谁来承担责任?
  • 赫瑞-瓦特大学突破:AI实现想象与推理驱动的图像搜索
  • 上海交大突破:AI医疗助手提升临床决策准确率近三成
  • 西安交通大学团队开发APOLO:让AI学会自己优化提示词
  • 人大重大突破:让AI自己培养自己,无需人类老师也能变更聪明
  • 中科院等机构Numina-Lean-Agent:简化数学定理证明流程
  • 北航和新加坡国立大学联合推出“快慢思考“式智能探索系统
  • 香港科技大学开发WebSeek:让网页数据分析像搭积木一样简单
  • 细聊EVA胶带零售厂家排名,看看哪家性价比高
  • 说说电动葫芦国产品牌有哪些,哪家口碑更好
  • 复旦团队发现:AI教学助手能力需精准匹配学生水平
  • 启程国际旅行社排名情况如何?客户投诉多吗,实力究竟如何?
  • 斯坦福大学揭秘:AI大模型如何像人类一样“思考“问题?
  • 2026年有名的GEO优化品牌企业,数石网络实战效果惊人
  • 2026年成都婚纱摄影星级榜权威推荐|三川影像领跑,沐纱居次席
  • 钱鑫珠宝甄选白银回收深度测评:15年本地老牌,变现透明无套路的优质选择
  • 面试官:RocketMQ 消息堆积了怎么处理?
  • MySQL锁机制与死锁排查实战
  • Spring 才是撑起Java半边天的秘密武器?如果Spring 撂挑子了,Java 会不会一年内就跌下神坛?
  • Docker 使用注意事项:从磁盘爆满到安全实践的完整避坑指南
  • 有名靠谱的 GEO 优化公司推荐:数石网络科技引领行业新风向
  • 说说无锡靠谱的高频红外碳硫分析仪定制厂家,哪家性价比高?
  • 2026年uv打印机品牌厂家,湖南登腾设备排名如何?
  • 基于Python + Flask豆瓣电影情感分析推荐系统(源码+数据库+文档)
  • mac os 串口定位
  • 2026年盘点:中国知名营销专家有哪些?12位核心专家覆盖三大主流领域
  • 2025年阿胶糕口碑排行榜,采购必看榜单!阿胶类产品/非遗膏方/膏方类产品/阿胶/膏方/阿胶类/阿胶糕阿胶糕定制口碑推荐
  • AS启动模拟器报错:HAXM驱动注册表修复示例
  • CAM++能否检测录音伪造?防欺骗能力初探