当 Laravel 请求规则无效时如何在 React 组件中显示错误消息 - Laravel Inertia React

问题描述 投票:0回答:1

当我的 Laravel 请求规则在 React 组件中无效时(本例中为电子邮件),我不知道如何显示错误消息。enter image description hereenter image description hereenter image description here

如果在我的 React 组件中不遵守 Laravel 电子邮件规则请求,我希望显示一条错误消息“您必须输入有效的电子邮件”。

reactjs laravel-8 inertiajs
1个回答
0
投票

你尝试过 useState 吗?

import { useState } from 'react';

const [isInvalidEmail, setIsInvalidEmail] = useState(false);

在您的代码决定需要检查此项的地方调用 setIsInvalidEmail,然后在页面上使用三元运算符显示 div:

<div>
  {isInvalidEmail ? "you have to enter a valid email" : ""}
<div>
© www.soinside.com 2019 - 2024. All rights reserved.