Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: useMemo for marks and steps #866

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
{
"name": "rc-slider",
"version": "10.1.1",
"name": "rc-slider-perf",
"version": "10.1.5",
"description": "Slider UI component for React",
"engines": {
"node": ">=8.x"
},
"keywords": [
"react",
"react-component",
Expand All @@ -13,40 +10,36 @@
"input",
"range"
],
"homepage": "http://github.com/react-component/slider/",
"homepage": "http://github.com/madzgo/rc-slider-perf/",
"bugs": {
"url": "http://github.com/madzgo/rc-slider-perf/issues"
},
"repository": {
"type": "git",
"url": "git@github.com:react-component/slider.git"
},
"bugs": {
"url": "http://github.com/react-component/slider/issues"
"url": "git@github.com:madzgo/rc-slider-perf.git"
},
"license": "MIT",
"main": "./lib/index",
"module": "./es/index",
"types": "./lib/index.d.ts",
"style": "./assets/index.css",
"files": [
"assets/*.css",
"lib",
"es",
"dist"
],
"license": "MIT",
"main": "./lib/index",
"module": "./es/index",
"style": "./assets/index.css",
"types": "./lib/index.d.ts",
"scripts": {
"start": "dumi dev",
"compile": "father build && lessc assets/index.less assets/index.css",
"coverage": "father test --coverage",
"docs:build": "dumi build",
"docs:deploy": "gh-pages -d .doc",
"compile": "father build && lessc assets/index.less assets/index.css",
"prepublishOnly": "npm run compile && np --yolo --no-publish",
"lint": "eslint src/ --ext .ts,.tsx,.jsx,.js,.md",
"now-build": "npm run docs:build",
"prepublishOnly": "npm run compile && np --yolo --no-publish",
"prettier": "prettier --write \"**/*.{ts,tsx,js,jsx,json,md}\"",
"test": "father test",
"coverage": "father test --coverage",
"now-build": "npm run docs:build"
},
"peerDependencies": {
"react": ">=16.9.0",
"react-dom": ">=16.9.0"
"start": "dumi dev",
"test": "father test"
},
"dependencies": {
"@babel/runtime": "^7.10.1",
Expand Down Expand Up @@ -79,5 +72,12 @@
"react-dom": "^16.0.0",
"regenerator-runtime": "^0.13.9",
"typescript": "^4.0.5"
},
"peerDependencies": {
"react": ">=16.9.0",
"react-dom": ">=16.9.0"
},
"engines": {
"node": ">=8.x"
}
}
9 changes: 1 addition & 8 deletions src/Marks/Mark.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ export interface MarkProps {
children?: React.ReactNode;
style?: React.CSSProperties;
value: number;
onClick: (value: number) => void;
}

export default function Mark(props: MarkProps) {
const { prefixCls, style, children, value, onClick } = props;
const { prefixCls, style, children, value } = props;
const { min, max, direction, includedStart, includedEnd, included } =
React.useContext(SliderContext);

Expand All @@ -30,12 +29,6 @@ export default function Mark(props: MarkProps) {
...positionStyle,
...style,
}}
onMouseDown={(e) => {
e.stopPropagation();
}}
onClick={() => {
onClick(value);
}}
>
{children}
</span>
Expand Down
11 changes: 2 additions & 9 deletions src/Marks/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ export interface InternalMarkObj extends MarkObj {
export interface MarksProps {
prefixCls: string;
marks?: InternalMarkObj[];
onClick: (value: number) => void;
}

export default function Marks(props: MarksProps) {
const { prefixCls, marks, onClick } = props;
const { prefixCls, marks } = props;

const markPrefixCls = `${prefixCls}-mark`;

Expand All @@ -29,13 +28,7 @@ export default function Marks(props: MarksProps) {
return (
<div className={markPrefixCls}>
{marks.map(({ value, style, label }) => (
<Mark
key={value}
prefixCls={markPrefixCls}
style={style}
value={value}
onClick={onClick}
>
<Mark key={value} prefixCls={markPrefixCls} style={style} value={value}>
{label}
</Mark>
))}
Expand Down
27 changes: 19 additions & 8 deletions src/Slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,23 @@ const Slider = React.forwardRef((props: SliderProps, ref: React.Ref<SliderRef>)
],
);

// Performance improvements to not rerender marks and steps while dragging
const marksElements = React.useMemo<React.ReactNode>(() => {
return <Marks prefixCls={prefixCls} marks={markList} />;
}, [markList, prefixCls]);

const stepsElements = React.useMemo<React.ReactNode>(() => {
return (
<Steps
prefixCls={prefixCls}
marks={markList}
dots={dots}
style={dotStyle}
activeStyle={activeDotStyle}
/>
);
}, [markList, prefixCls, dots, dotStyle, activeDotStyle]);

// ============================ Render ============================
return (
<SliderContext.Provider value={context}>
Expand All @@ -500,13 +517,7 @@ const Slider = React.forwardRef((props: SliderProps, ref: React.Ref<SliderRef>)
onStartMove={mergedDraggableTrack ? onStartMove : null}
/>

<Steps
prefixCls={prefixCls}
marks={markList}
dots={dots}
style={dotStyle}
activeStyle={activeDotStyle}
/>
{stepsElements}

<Handles
ref={handlesRef}
Expand All @@ -521,7 +532,7 @@ const Slider = React.forwardRef((props: SliderProps, ref: React.Ref<SliderRef>)
handleRender={handleRender}
/>

<Marks prefixCls={prefixCls} marks={markList} onClick={changeToCloseValue} />
{marksElements}
</div>
</SliderContext.Provider>
);
Expand Down