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

تغيير الحالة في طرف العميل لا يحصل إلا بعد تحديث الصفحة.

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

السؤال

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

أنا بصدد العمل على مشروع MERN وأستعمل redux toolkit لإدارة حالة التطبيق.

في المثال التالي:

1.JPG.647b25fcc517f6d637eaededcc94cabe.JPG

عند الضغط على زر الحذف، لايتم التفعيل إلا بعد تحديث الصفحة.

الكود:

slice

removeEducation(state, action) {
      state.profile.education = state.profile.education.filter(
        (el) => el._id !== action.payload
      );
    },

api call

export function deleteEducation(educId) {
  return async (dispatch, getState) => {
    try {
      const { data } = await axios.delete(
        `${PROFILE_URL}/education/${educId}`,
        {
          headers: {
            "x-auth-token": getState().auth.user.token,
          },
        }
      );
      dispatch(profileActions.removeEducation(data));
      // dispatch(profileActions.setProfile())
    } catch (error) {
      const errors = error.response.data.errors;
      errors?.forEach((err) => {
        dispatch(alertActions.createAlert(err.msg));
        dispatch(alertActions.clearAlert(err.id));
      });
      dispatch(profileActions.clearLoading());
      console.log(error);
    }
  };
}

المكون Education

const handleRemoveEducation = (id) => {
    dispatch(deleteEducation(id));
  };

**********************
{profile?.education.map((educ, index) => (
                      <tr key={index} className="bg-gray-100 border-b">
                        <td className="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
                          {index + 1}
                        </td>
                        <td className="text-sm text-gray-900 font-light px-6 py-4 whitespace-nowrap">
                          {educ.school}
                        </td>
                        <td className="text-sm text-gray-900 font-light px-6 py-4 whitespace-nowrap">
                          {educ.degree}
                        </td>
                        <td className="text-sm text-gray-900 font-light px-6 py-4 whitespace-nowrap">
                          {educ.current
                            ? `${moment(educ.from).format(
                                "DD MMM YYYY"
                              )} - present`
                            : `${moment(educ.from).format(
                                "DD MMM YYYY"
                              )} - ${moment(educ.to).format("DD MMM YYYY")}`}
                        </td>
                        <td className="text-sm text-gray-900 font-light px-6 py-4 whitespace-nowrap">
                          <Button
                            dangerBtn
                            type={"button"}
                            onClick={() => handleRemoveEducation(educ._id)}
                          >
                            Delete
                          </Button>
                        </td>
                      </tr>
                    ))}

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

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

Recommended Posts

  • 0

مرحبا محمود 
من الملاحظ أنه فى المكون Education يتم عملية الإرسال dispatch للتابع deleteEducation(id) فقط 

const handleRemoveEducation = (id) => {
    dispatch(deleteEducation(id));
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  };

والتابع deleteEducation(id) يقوم بالإتصال بالخادم والإزالة من قاعدة البيانات وهذا يتم بشكل صحيح وتظهر نتيجته بعد عملية التحديث للصفحة.
ولكن لابد أن تتم عملية الإرسال dispatch للتابع (id)removeEducation لكى يتم التفعيل بدون الحاجة لتحديث الصفحة كالأتى

const handleRemoveEducation = (id) => {
    dispatch(deleteEducation(id));
    dispatch(removeEducation(id));
};

بالتوفيق..

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

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

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

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

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   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.

  • إعلانات

  • تابعنا على



×
×
  • أضف...