Repository: ant-design/ant-design-mobile (ts)

Issue:
当form设置了initialValues时会导致Form.Subscribe 里面的方法执行两次
### Version of antd-mobile

5.29.0

### Operating system and its version

iOS

### Browser and its version

Google Chrome 版本 111.0.5563.146（正式版本） (arm64)

### Sandbox to reproduce

https://codesandbox.io/s/weathered-glade-3jnry8?file=/app.tsx:1305-1316

### What happened?

希望只执行一次

### Relevant log output

_No response_

Pull request description:
fix(Form): opt form Form.Subscribe do not call useless rerender
resolve #6088

Patch:
diff --git a/src/components/form/form-subscribe.tsx b/src/components/form/form-subscribe.tsx
index 8e69be967..3b3a77de1 100644
--- a/src/components/form/form-subscribe.tsx
+++ b/src/components/form/form-subscribe.tsx
@@ -19,9 +19,18 @@ export interface FormSubscribeProps {
 export const FormSubscribe: FC<FormSubscribeProps> = props => {
   const update = useUpdate()
   const form = useContext(FieldContext)
+
+  const value = form.getFieldsValue(props.to)
+
+  // Memo to avoid useless render
+  const childNode = React.useMemo(
+    () => props.children(value, form),
+    [JSON.stringify(value)]
+  )
+
   return (
     <>
-      {props.children(form.getFieldsValue(props.to), form)}
+      {childNode}
       {props.to.map(namePath => (
         <Watcher
           key={namePath.toString()}


Test patch:
diff --git a/src/components/form/tests/form.test.tsx b/src/components/form/tests/form.test.tsx
index 5b44e9fbf..cb9c0ba7f 100644
--- a/src/components/form/tests/form.test.tsx
+++ b/src/components/form/tests/form.test.tsx
@@ -303,4 +303,31 @@ describe('Form', () => {
       expect(onChange).toHaveBeenCalled()
     })
   })
+
+  describe('Form.Subscribe', () => {
+    test('no useless render', () => {
+      let renderTimes = 0
+
+      const { container } = render(
+        <Form initialValues={{ name: 'bamboo' }}>
+          <Form.Subscribe to={['name']}>
+            {({ name }) => {
+              renderTimes += 1
+
+              expect(name).toEqual('bamboo')
+
+              return (
+                <Form.Item name='name'>
+                  <Input />
+                </Form.Item>
+              )
+            }}
+          </Form.Subscribe>
+        </Form>
+      )
+
+      expect(container.querySelector('input')?.value).toEqual('bamboo')
+      expect(renderTimes).toEqual(1)
+    })
+  })
 })