首页 > 解决方案 > 如何将此类扩展到 redux?

问题描述

我想像我的其他组件一样将这个类连接到 redux。这个类只负责计算增益值并将它们发送到蓝牙设备。

这就是我通常在我的 React Native 应用程序中连接页面/组件的方式:

import React, { Component } from 'react';
import {
    View,
    Image,
    Keyboard,
    TouchableOpacity,
    StyleSheet,
    Alert
} from 'react-native';
import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';
import { connect } from 'react-redux';

class SigninPage extends Component {
    constructor(props) {
        super(props);
        this.state = {
            loadingVisible: false,
            passwordVisible: false,
            isLoginSuccess: false,
        };
    }

    async componentDidMount() {
        const bluetoothIsOn = await ConnectivityManager.isBluetoothEnabled()
        this.props.isBTConnectedFunc(bluetoothIsOn);
    }
    componentWillUnmount() {
        this.clearSigninTimeout();
    }
    more methods and render below


}


const mapStateToProps = (state) => {
    return {

    }
}
const mapDispatchToProps = (dispatch) => {
    return {

        }
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(SigninPage);

但是,当我尝试连接这个类时,我得到一个错误。

import SOMEBluetoothManager, { SOMEBluetoothEventEmitter } from '../nativeComponents/SOMEBluetooth/SOMEBluetooth';
import { connect } from 'react-redux';

const MAX_PAYLOAD_LENGTH = 14;

class SOMEDataTransfer {
    constructor(gainParamList) {
        this.gainParamList = gainParamList;
        this.transferTimeoutDuration = 10000;
        // console.log('lets llook again at the gainparamlist that is being sent over inside the constructor ', gainParamList);
    }
    start(onFinishCallback) {
        this.onFinishCallback = onFinishCallback;
        this.setupBluetoothEventListeners();

        let initialChannel = 0;
        let initialGains = this.getChannelGains(initialChannel);

        let transferChannelGainsCompleteCallback = (success, channel) => {
            if (success) {
                let newChannel = channel + 1;
                if (newChannel >= this.gainParamList.length) {
                    // console.log('Finished! and now lets look at success and channell, onFinishcallback', success, channel, onFinishCallback);
                    this.removeBluetoothEventListeners();
                    if (onFinishCallback) {
                        this.onFinishCallback(true);
                    }
                }
                else {
                    console.log('Channel ' + channel + ' transfer complete');
                    let gains = this.getChannelGains(newChannel);
                    this.transferChannelGains(gains, newChannel, 0, transferChannelGainsCompleteCallback);
                }
            }
            else {
                console.log('Channel ' + channel + ' transfer failed');
                this.removeBluetoothEventListeners();
                if (onFinishCallback) {
                    this.onFinishCallback(false);
                }
            }
        }
        this.transferChannelGains(initialGains, initialChannel, 0, transferChannelGainsCompleteCallback);
    }
}
const mapStateToProps = (state) => {
    return {

    }
}
const mapDispatchToProps = (dispatch) => {
    return {

    }
}
export default connect(mapStateToProps, mapDispatchToProps)(SOMEDataTransfer);

当我尝试通过连接连接这个类时。我收到此错误: 在此处输入图像描述

我想如何创建一个动作/减速器,以便我可以在应用程序的任何位置设置和访问增益值。

标签: javascriptclassredux

解决方案


我认为,您不需要该connect方法,因为它是为 React 应用程序制作的。store.dispatch相反,请使用 store 方法,例如:store.subscribestore.get.

您可以按照本教程进行操作。


推荐阅读