简体   繁体   English

如何访问类函数内部的状态?

[英]How to access the state inside the class function?

I need to access the redux state inside that function, so I can call Translator.init(data) wherever I want to use that.我需要访问该函数内的 redux 状态,因此我可以在任何我想使用它的地方调用 Translator.init(data)。 But I couldn't access the state like this.但是我无法像这样访问状态。 I'm not familiar with class components, What's wrong here?我不熟悉类组件,这里有什么问题?

    import { connect } from 'react-redux';
    import { createStructuredSelector } from 'reselect';
    import { makeSelectNumber } from './selectors';

      class Translator {
          init(data) {
            const sum = data + this.props.num
            return sum;
         }
    }

const mapStateToProps = createStructuredSelector({
  num: makeSelectNumber(),
});

export default connect(mapStateToProps)(Translator);

export const TranslatorService = new Translator();

Did you imported the correct selector?.您是否导入了正确的选择器? You are importing makeSelectLocale and calling makeSelectNumber inside mapStateToProps?您正在导入 makeSelectLocale 并在 mapStateToProps 中调用 makeSelectNumber?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM