skip to Main Content

I need to implement error handling in my application. I am adding a user group with a pop-up. After sending a request, I need to show an error message if it comes from the backend. However, I’m having trouble understanding JavaScript async functions. I created a useEffect hook, and if there is an error, I set it using useState and display it in the pop-up. But if there is no error, I want to close the pop-up. The problem is that the error is initially null, so there is no side effect created, and I cannot control it with an if statement. Only after receiving some errors and then sending a new request that doesn’t throw an error, the pop-up closes. I believe the issue lies in the useEffect hook. Maybe I should initialize the error in the beginning? Any help would be greatly appreciated. Here’s the relevant code:

I’m encountering an issue where the error is already null in the beginning, so it doesn’t create a side effect and I can’t control it with the if statement. I want to be able to close the pop-up when there is no error. How can I achieve this?

const RoleList: React.FC<RoleListComponentProps> = (props) => {
    useWebservice(true, loadUserRoles);
    const roles = useSelector(selectUserRoles);
    const selectedRole = useSelector(selectSelectedRole);
    let roleError: string | null = useSelector(selectUserRoleErrorMessage);
    const msalContext: IMsalContext = useMsal();
    const dispatch = useDispatch();

    // ...other state and functions...


    useEffect(() => {
        if (typeof roleError === "string" && roleError === "Employees: Request failed with status code 409") {
            setGroupNameError("This user group is already created before!");
        } else if (typeof roleError === "string") {
            setGroupNameError(roleError);
        } else {
            handleCloseAddGroupPopup();
        }
    }, [dispatch, roleError, handleCloseAddGroupPopup]);

    return (
        <ListComponent
            // ...other props...
        />
    );
};

export default RoleList;

2

Answers


  1. The issue lies in the fact that the initial value of roleError is null, so the initial render of your component doesn’t trigger the effect.

    Possible Solution

    initialize roleError as an empty string, the effect will be triggered on the initial render. This allows you to handle the case when there is no error and close the pop-up accordingly.

    Login or Signup to reply.
  2. There is a lot going on in this component and you are mixing multiple different approaches to get to your goal.

    First:
    The values in the square brackets are somewhat of a cache.
    The use effect "listens" to this values and if they change, the effect is triggered.

    In your case, the dispatch and handleCloseAddGroupPopup should most likely not be in this array. More to this here

    I assume you are using a dependency like redux to get the values from outside the render components?
    If so, all the error handling should be done there.
    The component should than receive all that should be rendered as props.

    If for some reason you are not able to close the dialog from where you make the call/create the error object, pass a concrete value down to the component that will close the dialog.

    This is an anti pattern, but should do the trick

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search