【山大会议】项目引入 Redux

前言

React 使用的是单向数据流,为了实现数据流在不同组件中实现共享,我为项目引入了 Redux 进行全局的数据状态管理。

安装依赖

首先需要安装 redux 核心依赖:

yarn add @reduxjs/toolkit

定义活动

我们在 src/Utils 下创建新的文件夹 Store,在其中新建文件 actions.js

/**
 * action 类型
 */
export const UPDATE_AVAILABLE_VIDEO_DEVICES = 'UPDATE_AVAILABLE_VIDEO_DEVICES'
export const UPDATE_AVAILABLE_AUDIO_DEVICES = 'UPDATE_AVAILABLE_AUDIO_DEVICES'
export const EXCHANGE_VIDEO_DEVICE = 'EXCHANGE_VIDEO_DEVICE'
export const EXCHANGE_AUDIO_DEVICE = 'EXCHANGE_AUDIO_DEVICE'

/**
 * 其他常量
 */
export const DEVICE_TYPE = {
    VIDEO_DEVICE: 'video',
    AUDIO_DEVICE: 'audio'
}

/**
 * action 活动
 */
export function updateAvailableDevices(deviceType, devices) {
    switch (deviceType) {
        case DEVICE_TYPE.VIDEO_DEVICE:
            return { type: UPDATE_AVAILABLE_VIDEO_DEVICES, devices }
        case DEVICE_TYPE.AUDIO_DEVICE:
            return { type: UPDATE_AVAILABLE_AUDIO_DEVICES, devices }
        default:
            return null
    }
}
export function exchangeMediaDevice(deviceType, deviceInfo) {
    switch (deviceType) {
        case DEVICE_TYPE.VIDEO_DEVICE:
            return { type: EXCHANGE_VIDEO_DEVICE, deviceInfo }
        case DEVICE_TYPE.AUDIO_DEVICE:
            return { type: EXCHANGE_AUDIO_DEVICE, deviceInfo }
        default:
            return null
    }
}

定义分发器

Store 文件夹下,创建 reducers.js

import { combineReducers } from '@reduxjs/toolkit';
import {
    UPDATE_AVAILABLE_VIDEO_DEVICES,
    UPDATE_AVAILABLE_AUDIO_DEVICES,
    EXCHANGE_AUDIO_DEVICE,
    EXCHANGE_VIDEO_DEVICE,
} from './actions'

function updateAvailableVideoDevices(state = [], action) {
    switch (action.type) {
        case UPDATE_AVAILABLE_VIDEO_DEVICES:
            action.devices.push()
            return action.devices
        default:
            return state
    }
}

function updateAvailableAudioDevices(state = [], action) {
    switch (action.type) {
        case UPDATE_AVAILABLE_AUDIO_DEVICES:
            return action.devices
        default:
            return state
    }
}

function exchangeVideoDevice(state = null, action) {
    switch (action.type) {
        case EXCHANGE_VIDEO_DEVICE:
            localStorage.setItem('usingVideoDevice', action.deviceInfo.key)
            return action.deviceInfo
        default:
            return state
    }
}

function exchangeAudioDevice(state = null, action) {
    switch (action.type) {
        case EXCHANGE_AUDIO_DEVICE:
            localStorage.setItem('usingAudioDevice', action.deviceInfo.key)
            return action.deviceInfo
        default:
            return state
    }
}

const reducers = combineReducers({
    availableVideoDevices: updateAvailableVideoDevices,
    availableAudioDevices: updateAvailableAudioDevices,
    usingVideoDevice: exchangeVideoDevice,
    usingAudioDevice: exchangeAudioDevice
})

export default reducers;

导出 Redux

Store 文件夹下,创建 store.js

import { configureStore } from '@reduxjs/toolkit';
import reducers from './reducers';

const store = configureStore({
    reducer: reducers,
});

export default store;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小栗帽今天吃什么

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值