forked from ant-design/ant-design
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Invalid memoized context value in LocaleProvider (ant-design#33723)
* fix: Invalid memoized context value in LocaleProvider * Add button type in test case. * fix: Invalid memoized context value in Anchor
- Loading branch information
Showing
4 changed files
with
121 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React, { memo, useState, useRef, useContext } from 'react'; | ||
import { mount } from 'enzyme'; | ||
import Anchor from '../Anchor'; | ||
import AnchorContext from '../context'; | ||
|
||
// we use'memo' here in order to only render inner component while context changed. | ||
const CacheInner = memo(() => { | ||
const countRef = useRef(0); | ||
countRef.current++; | ||
// subscribe anchor context | ||
useContext(AnchorContext); | ||
return ( | ||
<div> | ||
Child Rendering Count: <span id="child_count">{countRef.current}</span> | ||
</div> | ||
); | ||
}); | ||
|
||
const CacheOuter = () => { | ||
// We use 'useState' here in order to trigger parent component rendering. | ||
const [count, setCount] = useState(1); | ||
const handleClick = () => { | ||
setCount(count + 1); | ||
}; | ||
// During each rendering phase, the cached context value returned from method 'Anchor#getMemoizedContextValue' will take effect. | ||
// So 'CacheInner' component won't rerender. | ||
return ( | ||
<div> | ||
<button type="button" onClick={handleClick} id="parent_btn"> | ||
Click | ||
</button> | ||
Parent Rendering Count: <span id="parent_count">{count}</span> | ||
<Anchor affix={false}> | ||
<CacheInner /> | ||
</Anchor> | ||
</div> | ||
); | ||
}; | ||
|
||
it("Rendering on Anchor without changed AnchorContext won't trigger rendering on child component.", () => { | ||
const wrapper = mount(<CacheOuter />); | ||
const childCount = wrapper.find('#child_count').text(); | ||
wrapper.find('#parent_btn').at(0).simulate('click'); | ||
expect(wrapper.find('#parent_count').text()).toBe('2'); | ||
// child component won't rerender | ||
expect(wrapper.find('#child_count').text()).toBe(childCount); | ||
wrapper.find('#parent_btn').at(0).simulate('click'); | ||
expect(wrapper.find('#parent_count').text()).toBe('3'); | ||
// child component won't rerender | ||
expect(wrapper.find('#child_count').text()).toBe(childCount); | ||
}); |
53 changes: 53 additions & 0 deletions
53
components/locale-provider/__tests__/cached-context.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import React, { memo, useState, useRef, useContext } from 'react'; | ||
import { mount } from 'enzyme'; | ||
import LocaleProvider, { ANT_MARK } from '..'; | ||
import LocaleContext from '../context'; | ||
|
||
const defaultLocale = { | ||
locale: 'locale', | ||
}; | ||
// we use'memo' here in order to only render inner component while context changed. | ||
const CacheInner = memo(() => { | ||
const countRef = useRef(0); | ||
countRef.current++; | ||
// subscribe locale context | ||
useContext(LocaleContext); | ||
return ( | ||
<div> | ||
Child Rendering Count: <span id="child_count">{countRef.current}</span> | ||
</div> | ||
); | ||
}); | ||
|
||
const CacheOuter = () => { | ||
// We use 'useState' here in order to trigger parent component rendering. | ||
const [count, setCount] = useState(1); | ||
const handleClick = () => { | ||
setCount(count + 1); | ||
}; | ||
// During each rendering phase, the cached context value returned from method 'LocaleProvider#getMemoizedContextValue' will take effect. | ||
// So 'CacheInner' component won't rerender. | ||
return ( | ||
<div> | ||
<button type="button" onClick={handleClick} id="parent_btn"> | ||
Click | ||
</button> | ||
Parent Rendering Count: <span id="parent_count">{count}</span> | ||
<LocaleProvider locale={defaultLocale} _ANT_MARK__={ANT_MARK}> | ||
<CacheInner /> | ||
</LocaleProvider> | ||
</div> | ||
); | ||
}; | ||
|
||
it("Rendering on LocaleProvider won't trigger rendering on child component.", () => { | ||
const wrapper = mount(<CacheOuter />); | ||
wrapper.find('#parent_btn').at(0).simulate('click'); | ||
expect(wrapper.find('#parent_count').text()).toBe('2'); | ||
// child component won't rerender | ||
expect(wrapper.find('#child_count').text()).toBe('1'); | ||
wrapper.find('#parent_btn').at(0).simulate('click'); | ||
expect(wrapper.find('#parent_count').text()).toBe('3'); | ||
// child component won't rerender | ||
expect(wrapper.find('#child_count').text()).toBe('1'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters