javascript - 如何将此类扩展到 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);
我想如何创建一个动作/减速器,以便我可以在应用程序的任何位置设置和访问增益值。
解决方案
我认为,您不需要该connect
方法,因为它是为 React 应用程序制作的。store.dispatch
相反,请使用 store 方法,例如:store.subscribe
和store.get
.
您可以按照本教程进行操作。
推荐阅读
- dataflow-diagram - 如何使用 Yourdon DeMarco 表示法在数据流程图 (DFD) 中表示重复的数据存储?
- c# - StreamReader 与 using 语句的区别?
- shell - /bin/sh: 解析命令行参数导致: shift: can't shift that many
- python - 如何找到 datetime、math、win32com.client 的版本?
- javascript - 将堆栈错误作为字符串时反转 blob
- postgresql - SQL | 同一张表中的父子关系
- laravel - 保存两个字段同名关系一对一背包laravel
- android - 在 Android App Bundle 构建上 Flutter android 应用程序崩溃
- c# - 向用户实体添加自定义字段
- javascript - 如何在具有不同键的json对象中添加所有值