How can I use react-redux useSelector in class component?

As @Ying Zuo said, your method works only with Functional Components. To solve this problem:

Instead of this line:

const counter = useSelector(state => state.counter);

You define the counter state like this:

const mapStateToProps = state => ({
    counter: state.counter
});

Then for dispatching you should use it like this:

const mapDispatchToProps = () => ({ 
    increment, 
    decrement
});

At the end you combine everything like this:

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

Don’t forget to import increment and decrement from your action and connect from the react-redux module.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)