Я создал проект react-redux и использовал json-server в качестве сервера. когда я создаю заказ, я сохраняю статус в UiReducer и использую его в «OrderStatusPage». NODE_ENV установлен на «развитие». Заказ добавлен в мой db.json, но я получил эту ошибку в «OrderStatusPage»:
Uncaught TypeError: react_jsx_dev_runtime__WEBPACK_IMPORTED_MODULE_4__.jsxDEV(...) не является функцией
как я могу решить эту ошибку? Большое спасибо.
import React from "react";
import { useSelector } from "react-redux";
export const OrderStatusPage = () => {
const notification = useSelector((state) => state.ui.notification);
return (
<div className = "container">
<div className = "row d-flex justify-content-center">
<div className = "col-md-6 my-5 text-center">
{notification &&
(<Notification
title = {notification.title}
message = {notification.message}
status = {notification.status}
/>)(notification.status === "success") ? (
<Button type = "button" >
Go to your Order
</Button>
) : (
<Button type = "button" >
Go to your Cart
</Button>
)}
</div>
</div>
</div>
);
};
Вы не должны изменять состояние напрямую. Попробуйте использовать состояние, например:
import React from "react";
import { useSelector } from "react-redux";
export const OrderStatusPage = () => {
const [uistate, setUistate]=React.useState()
React.useEffect(()=>{
setUistate(useSelector((state) => state.ui.notification));
},[])
return (
<div className = "container">
<div className = "row d-flex justify-content-center">
<div className = "col-md-6 my-5 text-center">
{uistate &&
(<Notification
title = {uistate.title}
message = {uistate.message}
status = {uistate.status}
/>)(uistate.status === "success") ? (
<Button type = "button" >
Go to your Order
</Button>
) : (
<Button type = "button" >
Go to your Cart
</Button>
)}
</div>
</div>
</div>
);
};
Я думаю, все в порядке. Возможно, вам просто нужно разделить два условия, которые у вас есть:
{notification &&
(<Notification
title = {notification.title}
message = {notification.message}
status = {notification.status}
/>)}
{notification.status === "success" ? (
<Button type = "button" >
Go to your Order
</Button>
) : (
<Button type = "button" >
Go to your Cart
</Button>
)}
Я думаю, это сработает.
Спасибо, это работает, и у меня есть еще одна проблема в uiReducer, и я ее решаю. Наконец-то теперь все в порядке.
Я рад это слышать ^-^
в вашем коде вы используете компоненты Notification
и Button
, но не импортируете эти компоненты.
import React from "react";
import { useSelector } from "react-redux";
import { Notification } from "./Notification";
import { Button } from "./Button";
export const OrderStatusPage = () => {
const notification = useSelector((state) => state.ui.notification);
return (
<div className = "container">
<div className = "row d-flex justify-content-center">
<div className = "col-md-6 my-5 text-center">
{notification && (
<Notification
title = {notification.title}
message = {notification.message}
status = {notification.status}
/>
)}
{notification.status === "success" ? (
<Button type = "button">Go to your Order</Button>
) : (
<Button type = "button">Go to your Cart</Button>
)}
</div>
</div>
</div>
);
};
Спасибо за ваш ответ, но я просто удалил их в stackoverflow, чтобы уменьшить количество строк кода.
Я знаю это, и я управляю своим состоянием с помощью redux, а не с помощью useState.