plotly / plotly/dash

Dash doesn't support areEqual() in React.memo() to judge wheather need to redraw

Open
#2,208 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug P3
Dominant language
Python
Stars
24.4k
Forks
2.3k
Avg merge
2d 7h
Merged PRs (30d)
13

Description

When I was developing the Dash component with React functional component, I found areEqual() doesn't work when I use React.memo(Component, [areEqual(prevProps, nextProps)]) to prevent some unnecessary redraw, in fact, once I set my areEqual() in React.memo(), the Dash component won't generateted success after build, here is a simple demo(Dash version: 2.6.1):

import { memo } from 'react';

const MemoDemo = (props) => {

    const {
        n_clicks_a,
        n_clicks_b,
        setProps
    } = props;

    return (
        <div >
            <button onClick={() => setProps({ n_clicks_a: n_clicks_a + 1 })}>{'button a'}</button>
            <button onClick={() => setProps({ n_clicks_b: n_clicks_b + 1 })}>{'button b'}</button>
            <span >
                {`n_clicks_a: ${n_clicks_a}  n_clicks_b: ${n_clicks_b}`}
            </span>
        </div>
    );
}

MemoDemo.propTypes = {
    id: PropTypes.string,

    style: PropTypes.object,

    className: PropTypes.string,

    // should trigger redraw
    n_clicks_a: PropTypes.number,

    // should not trigger redraw
    n_clicks_b: PropTypes.number,

    setProps: PropTypes.func
};

MemoDemo.defaultProps = {
    n_clicks_a: 0,
    n_clicks_b: 0
}

export default memo(MemoDemo, (prevProps, nextProps) => {
    if (prevProps.n_clicks_a === nextProps.n_clicks_a) {
        // should not redraw
        return true;
    }
    // should redraw
    return false;
});

@T4rk1n

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the provided React.memo component example with Dash 2.6.1 and inspect the React component build path involved in generating Dash components. Confirm how the areEqual comparator is handled during the build, then verify that a component using it builds successfully and that redraw behavior follows the comparator.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.