From 3493250dfcf045e9cbb6fe8f21fff46a80066ca5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E7=88=B1=E5=90=83=E7=99=BD=E8=90=9D?= =?UTF-8?q?=E5=8D=9C?= Date: Thu, 8 Aug 2024 15:08:28 +0800 Subject: [PATCH] fix: preserve false should not trigger shouldUpdate rerender (#714) * test: test driven * enhance: shouldUpdate false should not trigger rerender --- src/Field.tsx | 5 ++++- tests/dependencies.test.tsx | 39 +++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/Field.tsx b/src/Field.tsx index 1b9d1cba3..964fff125 100644 --- a/src/Field.tsx +++ b/src/Field.tsx @@ -297,7 +297,10 @@ class Field extends React.Component implements F * - Reset A, need clean B, C */ case 'remove': { - if (shouldUpdate) { + if ( + shouldUpdate && + requireUpdate(shouldUpdate, prevStore, store, prevValue, curValue, info) + ) { this.reRender(); return; } diff --git a/tests/dependencies.test.tsx b/tests/dependencies.test.tsx index 82d986a3e..a06facd3b 100644 --- a/tests/dependencies.test.tsx +++ b/tests/dependencies.test.tsx @@ -226,4 +226,43 @@ describe('Form.Dependencies', () => { // sync end expect(spy).toHaveBeenCalledTimes(3); }); + + it('shouldUpdate false should not update', () => { + let counter = 0; + const formRef = React.createRef(); + + const { container } = render( +
+ + + + + prev.little !== next.little}> + {(_, __, form) => { + // Fill to hide + if (!form.getFieldValue('little')) { + return ; + } + + return null; + }} + + + false}> + {() => { + console.log('render!'); + counter += 1; + return null; + }} + +
, + ); + expect(counter).toEqual(1); + expect(container.querySelectorAll('input')).toHaveLength(2); + + // hide should not re-render + fireEvent.change(getInput(container, 0), { target: { value: '1' } }); + expect(container.querySelectorAll('input')).toHaveLength(1); + expect(counter).toEqual(1); + }); });