اذهب إلى المحتوى
  • 0

كيف يمكنني إخفاء عنصر في ريآكت بعد وقت محدد باستعال redux toolkit

محمود سعداوي2

السؤال

السلام عليكم.

بعد إظهار رسالات الخطأ أود إخفاءها باستعمال setTimeOut قمت بالكود التالي

const alertSlice = createSlice({
  name: "alert",
  initialState: {
    alerts: [],
  },
  reducers: {
    setAlerts(state, action) {
        state.alerts = [...state.alerts, action.payload]
    },
// الدالة التي تقوم بإزالة العنصر
    removeAlert(state, action) {
        state.alerts.filter(alert => alert.id === action.payload?.id)
    }
  },
});
***********************************************************************
const onSubmit = async e => {
    e.preventDefault()
    if (password !== password2) {
      dispatch(alertActions.setAlerts({
        id: new Date().getTime(),
        type: "danger",
        msg:"Passwords does not mutch"
      }))
    }
    setTimeout(()=> dispatch(alertActions.removeAlert()), 3000)
    console.log("success")
  }

عند تفعيل onSubmit تظهر رسالة الخطأ و ما أريده هو حذف الرسالة بعد 3 ثواني

شكرا على المساعدة.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

يمكنك القيام بذلك كالتالي :

const onSubmit = async e => {
  e.preventDefault()
  if (password !== password2) {
    const error = {
      id: new Date().getTime(),
      type: "danger",
      msg:"Passwords does not mutch"
    };
    dispatch(alertActions.setAlerts(error))
    setTimeout(()=> dispatch(alertActions.removeAlert(error)), 3000)
  }    
  console.log("success")
}

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...