Manage state with React Hooks

React Hooks enable you to use state, execute effects, and other React features without writing a class. You can use hooks to subscribe to the Redux store and dispatch actions, without having to wrap your components in connect().

If you use containers, you need to:
  • Use a React Class Component.
  • Implement mapDispatchToProps to have access to the dispatch object to call actions.
  • Implement mapStateToProps to have access to the state object to call selectors.
  • Use the connect higher-order component when you export the component to wire it with Redux.

For example:

class SampleContainer extends Component {

    componentDidMount() {
        //Initializations
        //Calling Action
        this.props.sampleAction();
    }

    ...

    render() {
        //Calling selector
        const selectorValue = this.props.sampleSelector();

        return <>Component body</>;
    }

    ...

}

// We need to implement this function to have access to the `dispatch` object
const mapDispatchToprops = dispatch => ({
    // Call actions using the dispatch object
    sampleAction: () => SampleModuleActions.actionName(dispatch);
})

// We need to implement this function to have access to the `state` object
const mapStateToProps = state => ({
    // Call selectors using the state object
    sampleSelector : () => SampleModuleSelectors.selectorName(state);
})

// To do the wiring with redux, we need to  use the `connect` HOC passing the two functions: `mapStateToProps` and `mapDispatchToProps`
export connect(mapStateToProps, mapDispatchToProps)(SampleContainer);

To do the same with hooks:

  • You don't use class components.
  • You don't need to use connect, mapStateToProps or mapDispatchToProps.
  • Use useDispatch to get the dispatch objects and call the actions.
  • Use useSelector to get the state object and call the selectors.
  • Use useEffect to simulate the life cycle events, for example componentDidMount

For example:

const SampleComponent  = props => {
    //Get the dispatch object to call actions
    const dispatch = useDispatch();

    // Initializations - The same as componentDidMount
    useEffect(() => {
        //Calling action
        SampleActions.actionName(dispatch);
    } , [])

    
    //To call the selectors you do:
    const selectorValue = useSelector(state => SampleSelectors.selectorName(state));

    return (<>Component body<>);

}

In addition to the reduced code, you can create custom hooks to further reduce the amount of code.

Custom hooks

The following custom login hooks are provided.

  • useGeneratedUserIfNotLoggedIn: On mounting a component, checks whether the user is logged in. If not, calls REST APIs to create a temporary user and automatically authenticate the user. This is useful for anonymous IEG forms.
  • usePublicCitizenIfNotLoggedIn: On mounting a component, checks whether the user is logged in. If not, automatically authenticate the user as a publicCitizen. For example, this is useful for landing pages that need to call REST APIs to populate lists.

If you don't want to log out the existing generated user, you can set the keepExistingGeneratedUser argument for these login hooks to true. By default, it is set to false.

It is not possible to implement these two custom functions without hooks, as a utility JavaScript file for example, because they need to modify the React component state.