所以我在使用 yup 进行条件验证时遇到问题。基本上,我希望在未切换复选框时运输具有所需的属性。我正在使用 yup 的when功能,但我不知道如何引用sameShippingAsBilling该shipping嵌套对象中的布尔值。
当复选框为 false 时,一切正常,显示错误...但是当选中复选框时,我收到此错误:"Cannot use 'in' operator to search for 'default' in undefined"
问题是is回调中的值未定义。是否可以从这些嵌套对象中访问外部变量when。有什么替代方法可以做到这一点?
这是沙箱示例
const schema = Yup.object({
sameShippingAsBilling: Yup.bool(),
billing: Yup.object({
line1: Yup.string().required(),
city: Yup.string().required(),
country: Yup.string().required()
}),
shipping: Yup.object({
line1: Yup.string().when("sameShippingAsBilling", {
is: !val || val === false,
then: Yup.string().required(),
}),
city: Yup.string().when("sameShippingAsBilling", {
is: !val || val === false,
then: Yup.string().required(),
}),
country: Yup.string().when("sameShippingAsBilling", {
is: !val || val === …Run Code Online (Sandbox Code Playgroud) 所以我有一个表单,其中包含我通过来自 react-hook-form 的 Field Array 添加的自定义字段。一切正常,但我为属性项添加了拖放功能(以重新排序),现在直接显示所有这些许多字段会很麻烦,所以我将它们移动到对话框中。
以下是了解什么更容易拖放的图片......(正确的)

问题是字段数组值在模式关闭后“重置”(在我在编辑模式中输入这些表单值之后),我猜这与重新渲染有关,但我不确定。
我试图在没有 d&d 和其他无用的东西的情况下展示最小的代码示例......
但这里是带有完整代码的代码和盒子游乐场
创建CategoryForm.js
const defaultValues = {
name: "",
slug: "",
description: "",
properties: [] // field array
}
function CreateCategoryForm() {
const methods = useForm({ defaultValues });
const { handleSubmit, control, errors } = methods;
const { fields, append, remove, swap } = useFieldArray({ name: "properties", control });
const onSubmit = async (data) => {
console.log("data: ", data);
};
return (
<Container>
<FormProvider {...methods}>
<form onSubmit={handleSubmit(onSubmit)} noValidate>
<FormTextField …Run Code Online (Sandbox Code Playgroud) 我正在使用下面的 clickOutside 操作。
export function clickOutside(node: HTMLElement) {
function detect({ target }: MouseEvent) {
if (!node.contains(target as Node)) {
node.dispatchEvent(new CustomEvent('clickoutside'));
}
}
document.addEventListener('click', detect, { passive: true, capture: true });
return {
destroy() {
document.removeEventListener('click', detect);
},
};
Run Code Online (Sandbox Code Playgroud)
当我将鼠标悬停在:clickoutside={() => {}} 上时,我仍然遇到此错误
Type '{ onclickoutside: () => void; class: string; }' is not assignable to type 'HTMLProps<HTMLDivElement>'.
Property 'onclickoutside' does not exist on type 'HTMLProps<HTMLDivElement>'}
Run Code Online (Sandbox Code Playgroud)
我在 svelte 套件中尝试过这个app.d.ts
declare namespace svelte.JSX {
interface HTMLAttributes<T> {
clickoutside?: (event: CustomEvent) …Run Code Online (Sandbox Code Playgroud) 我遇到了一个问题,需要你的帮助。我需要在名为 rudder 的软件中使用自动注册服务器的 API。为了达到我的目标,我必须使用 2 个 http 请求:
我的第一个请求得到了完美处理,但我不知道如何实现第二个请求。
这是我的代码(/src/controllers/registration.js):
async function index(req, res) {
// Check parameter
if (!req.body.hostname) {
return res.status(422).json({ result: 'error', message: 'Missing hostname parameter' });
}
try {
const pendingNodes = await axios.get(`${config.rudderURL}/rudder/api/latest/nodes/pending?include=minimal`, { headers });
Object.keys(pendingNodes.data.data.nodes).forEach((key) => {
// Search in all pending machine if our node is present
const node = pendingNodes.data.data.nodes[key];
if (req.body.hostname === node.hostname) {
// Registration
const response = async axios.get(`${config.rudderURL}/rudder/api/nodes/pending/${node.id}`, { headers });
// console.log(response);
return …Run Code Online (Sandbox Code Playgroud) javascript ×3
reactjs ×2
async-await ×1
axios ×1
forms ×1
material-ui ×1
node.js ×1
svelte ×1
sveltekit ×1
typescript ×1
yup ×1