如何将此类扩展到redux?

时间:2019-05-21 07:07:45

标签: javascript class redux

我想像其他组件一样将此类连接到redux。此类仅负责计算增益值并将其发送到Bluetooth设备。

这通常是我在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);

当我尝试通过连接连接此类时。我收到此错误: enter image description here

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

1 个答案:

答案 0 :(得分:0)

我认为,您不需要connect方法,因为它是为React应用程序而设计的。 而是使用诸如store.dispatchstore.subscribestore.get之类的存储方法。

您可以遵循此 tutorial