plotly / plotly/dash

Ref not being passed between wrapped parent and child components

Open
#2,571 1 comment 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

Describe your context

dash                 2.9.3
dash-core-components 2.0.0
dash-html-components 2.0.0
dash-table           5.0.0

Describe the bug

Within custom components created using the dash-component-boilerplate, passing a Ref between a wrapped parent component and a wrapped child component does not work. An example setup looks like the below.

Tooltip.react.js

import React, { Component } from "react";
import PropTypes from "prop-types";
import { ExternalTooltipComponent } from "@somelibrary";

export default class Tooltip extends Component {
    constructor(props) {
        super(props);
    }

    render() {
        const {id, setProps, children, ...rest} = this.props;

        return (
            <ExternalTooltipComponent
                id={id}
                {...rest}
            >
                { children }
            </ExternalTooltipComponent>
        );
    }
}

IconButton.react.js

import React, { Component, forwardRef } from "react";
import PropTypes from "prop-types";
import { ExternalIconButtonComponent } from "@somelibrary";

class IconButtonComponent extends Component {
    constructor(props) {
        super(props);

        this.onClick = this.onClick.bind(this);
    }

    onClick() {
        this.props.setProps({ n_clicks: this.props.n_clicks + 1 });
    }
 
    render() {
        const {id, setProps, innerRef, ...rest} = this.props;

        return (
            <ExternalIconButtonComponent
                id={id}
                ref={innerRef}
                onClick={this.onClick}
                {...rest}
            />
        );
    }
}

const IconButton = forwardRef(function IconButton(props, ref) {
    return <IconButtonComponent innerRef={ref} {...props} />;
});
export default IconButton;

Expected behavior

The Ref is available within the child component.

Notes

This may not be the best way of using forwardRef, particularly in the context of Dash. If there is a better or cleaner way to do this, I'd love to hear about it

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 with the Tooltip.react.js and IconButton.react.js examples and the dash-component-boilerplate's wrapper and ref-handling entry points. Reproduce the nested wrapped parent/child case, trace where the ref stops being available, and verify that the child can access the expected ref without breaking Dash props behavior.

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.