Skip to content

perf: nested Trigger should not force render when ancestor Trigger re… #270

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

Merged
merged 1 commit into from
Jun 7, 2021
Merged
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
15 changes: 5 additions & 10 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -780,15 +780,12 @@ export function generateTrigger(
this.setPopupVisible(false);
}

triggerContextValue = { onPopupMouseDown: this.onPopupMouseDown };

render() {
const { popupVisible } = this.state;
const {
children,
forceRender,
alignPoint,
className,
autoDestroy,
} = this.props;
const { children, forceRender, alignPoint, className, autoDestroy } =
this.props;
const child = React.Children.only(children) as React.ReactElement;
const newChildProps: HTMLAttributes<HTMLElement> & { key: string } = {
key: 'trigger',
Expand Down Expand Up @@ -877,9 +874,7 @@ export function generateTrigger(
}

return (
<TriggerContext.Provider
value={{ onPopupMouseDown: this.onPopupMouseDown }}
>
<TriggerContext.Provider value={this.triggerContextValue}>
{trigger}
{portal}
</TriggerContext.Provider>
Expand Down
61 changes: 61 additions & 0 deletions tests/basic.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -749,4 +749,65 @@ describe('Trigger.Basic', () => {
document.body.removeChild(div);
document.body.removeChild(root);
});

it('nested Trigger should not force render when ancestor Trigger render', () => {
let isUpdate = false;
let isChildUpdate = false;
let isGrandsonUpdate = false;

const Grandson = () => {
if (isUpdate) {
isGrandsonUpdate = true;
}

return null;
};

const Child = React.memo(() => {
if (isUpdate) {
isChildUpdate = true;
}

return (
<Trigger
action={['click']}
popupAlign={placementAlignMap.left}
forceRender
popup={() => (
<strong className="x-content">
<Grandson />
</strong>
)}
>
<div className="target">click</div>
</Trigger>
);
});

class App extends React.Component {
render() {
return (
<Trigger
action={['click']}
popupAlign={placementAlignMap.left}
popup={<strong className="x-content">tooltip2</strong>}
className="className-in-trigger-2"
>
<div className="target">
<Child />
</div>
</Trigger>
);
}
}

const wrapper = mount(<App />);

isUpdate = true;

wrapper.setState({});

expect(isChildUpdate).toBeFalsy();
expect(isGrandsonUpdate).toBeFalsy();
});
});