-
如题,v2中的生命周期钩子不能嵌套使用,比如:onFieldMount中就不能再使用onFieldChange。 场景1: 当一个card组件如果配置了flag为true,所有子级均隐藏时,需要将card组件本身也隐藏。 场景2: |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
重新设计代码满足了需求 |
Beta Was this translation helpful? Give feedback.
-
第一个问题的解法 import React from "react";
import {
createForm,
onFieldReact
} from "@formily/core";
import { FormProvider, createSchemaField } from "@formily/react";
import { Input, Button, Card } from "antd";
import "antd/lib/input/style";
const form = createForm({
effects() {
onFieldReact("input", (field) => {
if (!form.mounted) return;
const visible = field.visible;
field.query("card").take((card) => {
card.visible = visible;
});
});
}
});
const SchemaField = createSchemaField({
components: {
Input,
Card
}
});
export default () => (
<FormProvider form={form}>
<SchemaField
schema={{
type: "object",
properties: {
card: {
type: "void",
"x-component": "Card",
"x-component-props": {
title: "card",
flag: true
},
properties: {
input: {
type: "string",
"x-component": "Input"
},
}
},
}
}}
></SchemaField>
<Button
onClick={() => {
const field = form.query("input").take();
field.visible = false;
}}
>
hidden
</Button>
<Button
onClick={() => {
const field = form.query("input").take();
field.visible = true;
}}
>
visible
</Button>
</FormProvider>
); |
Beta Was this translation helpful? Give feedback.
-
第二个问题的解法 effects(){
customHook(()=>{
if(form.mounted && form.query('input').getIn('component[1].flag')){
//...
}
})
} |
Beta Was this translation helpful? Give feedback.
第一个问题的解法