javascriptreactjsredux

mapStateToProps for functional component


Is there any function similar to mapStateToProps so that we can connect Redux state to the functional component in React?

Is passing the state as props from parent component the only solution?


Solution

  • You can definitely use mapStateToProps with a functional component, the same way you would with a class component.

    function MyComponent({ propOne }) {
      return <p>{propOne}</p>
    }
    
    function mapStateToProps(state) {
      return { propOne: state.propOne };
    } 
    
    export default connect(mapStateToProps)(MyComponent);