0

I am trying to add alert component to my notebook App so when user logins and make any changes he gets alert at the top, To execute this I used context API, but when I am trying to access the state object to Alert component from AlertContext file, I get this below error

alert.js:10 Uncaught TypeError: Cannot read properties of undefined (reading 'type')

Here is my Alert and AlertContext files

import React from "react"; 
import { useContext } from "react"; 
import { AlertContext } from "../context/notes/AlertContext";
    
export const Alert = (props) => { 
const { alerts } = useContext(AlertContext);
return (
        <div>
          <div
            className={`alert alert-${alerts.type}  alert-dismissible fade show`}
            role="alert"
            role="alert"
          >
            {alerts.message}
          </div>
        </div>   
      ); 
};

import React, { useState } from 'react'
import { AlertContext } from './AlertContext'


const AlertState = (props) => {

  const [alerts, setAlerts] = useState({ message: null, type: null });

  const setMsg = (msg,type) =>
  {
    setAlerts({ message: msg, type: type });
    setTimeout(() =>
    {
      setAlerts({ message: null, type: null })
    },1500);
  }
  return (
      <AlertContext.Provider  value={setMsg , alerts}>
          {props.children}
      </AlertContext.Provider>
  );
}

export default AlertState
8
  • Are you wrapping the Alert component within your provider, in this case the AlertState component? Commented Feb 27, 2022 at 10:15
  • 2
    And it seems like you're missing brackets on this line: <AlertContext.Provider value={setMsg , alerts}>, it's supposed to be <AlertContext.Provider value={{setMsg , alerts}}> Commented Feb 27, 2022 at 10:16
  • Good observation, thank you for your time Commented Feb 27, 2022 at 10:19
  • 1
    In order for your component (in your case the Alert component) to be able to use React context, it has to have the Provider as its parent (in your case AlertContext.Provider, or more specifically your AlertState component). I mean do you have something like this in your component hierarchy: <AlertState><Alert /></AlertState>? Commented Feb 27, 2022 at 10:22
  • Yes, I have this one, otherwise I would have got some different error. Commented Feb 27, 2022 at 10:25

2 Answers 2

2

Try it

<AlertContext.Provider value={{setMsg,alerts}}>
         {props.children}
     </AlertContext.Provider>
Sign up to request clarification or add additional context in comments.

Comments

0

Add the following check in your Alert component beacuse initially the 'alerts' object is going to be null. That's why it is giving this error. Adding this check will resolve the error.

import React, { useContext } from "react";
import AlertContext from "../context/AlertContext";

const Alert = () => {
  const {alerts} = useContext(AlertContext);

  return alerts !== null ? (
    <div
      className={`alert alert-${alerts.type} alert-dismissible fade show`}
      role="alert"
    >
      {alerts.message}
    </div>
  ) : (
    <></>
  );
};

export default Alert;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.