简体   繁体   中英

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. 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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