-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(charts): Add customMemo for charts based on @ant-design/charts
- Loading branch information
1 parent
d07096c
commit 15ae303
Showing
16 changed files
with
127 additions
and
13 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
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
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
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
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,86 @@ | ||
import React, { useState, useCallback } from 'react'; | ||
import { Pie } from '@oceanbase/charts'; | ||
|
||
const Demo: React.FC = () => { | ||
const [count, setCount] = useState(1); | ||
const [data, setData] = useState([ | ||
{ | ||
type: '分类一', | ||
value: 27, | ||
}, | ||
{ | ||
type: '分类二', | ||
value: 25, | ||
}, | ||
{ | ||
type: '分类三', | ||
value: 18, | ||
}, | ||
{ | ||
type: '分类四', | ||
value: 15, | ||
}, | ||
{ | ||
type: '分类五', | ||
value: 10, | ||
}, | ||
{ | ||
type: '其他', | ||
value: 5, | ||
}, | ||
]); | ||
|
||
// memo function to avoid re-render | ||
const onReady = useCallback(plot => { | ||
console.log(plot); | ||
}, []); | ||
|
||
const config = { | ||
data, | ||
angleField: 'value', | ||
colorField: 'type', | ||
onReady, | ||
}; | ||
|
||
return ( | ||
<div> | ||
<span>{count}</span> | ||
<button | ||
onClick={() => { | ||
setCount(count + 1); | ||
}} | ||
style={{ | ||
marginLeft: 16, | ||
}} | ||
> | ||
外部状态改变不会重新渲染 | ||
</button> | ||
<button | ||
onClick={() => { | ||
setData([ | ||
{ | ||
type: '分类四', | ||
value: 15, | ||
}, | ||
{ | ||
type: '分类五', | ||
value: 10, | ||
}, | ||
{ | ||
type: '其他', | ||
value: Math.random() * 100, | ||
}, | ||
]); | ||
}} | ||
style={{ | ||
marginLeft: 16, | ||
}} | ||
> | ||
数据改变重新渲染 | ||
</button> | ||
<Pie {...config} /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Demo; |
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
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
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
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,13 @@ | ||
import { memo } from 'react'; | ||
import type { FunctionComponent } from 'react'; | ||
import { isEqual } from 'lodash'; | ||
import type { BaseConfig, AllBaseConfig } from '@ant-design/charts'; | ||
|
||
export function customMemo<P extends BaseConfig<AllBaseConfig>>( | ||
Component: FunctionComponent<P>, | ||
propsAreEqual?: (prevProps: Readonly<P>, nextProps: Readonly<P>) => boolean | ||
) { | ||
return memo(Component, (prevProps, nextProps) => | ||
propsAreEqual ? propsAreEqual(prevProps, nextProps) : isEqual(prevProps, nextProps) | ||
); | ||
} |