question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

withTheme does not forward ref

See original GitHub issue

Description

The withTheme HOC is not forwarding it’s ref. Therefore it is e.g. not possible to submit a form programmatically. React.forwardRef is available from 16.3.0, an implementation would look like this:

import React, { forwardRef } from "react";
import PropTypes from "prop-types";
import Form from "./";

function withTheme(themeProps) {
  return forwardRef(({ fields, widgets, ...directProps }, ref) => (
    <Form
      {...themeProps}
      {...directProps}
      fields={{ ...themeProps.fields, ...fields }}
      widgets={{ ...themeProps.widgets, ...widgets }}
      ref={ref}
    />
  ));
}

withTheme.propTypes = {
  widgets: PropTypes.object,
  fields: PropTypes.object,
};

export default withTheme;

For now I will use my own HOC instead of withTheme. Please fix this as soon as you bump the React peer version. I will happily provide an PR then, testcase also kinda done:

  it('should forward the ref', () => {
    let ref = undefined;
    const schema = {};
    const uiSchema = {};

    createComponent(withTheme({ ref: (form) => ref = form }), {
      schema,
      uiSchema,
    });

    expect(ref.submit).to.exist();
  });

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
zepatrikcommented, Jul 15, 2020

@jorisw pr is opened

1reaction
david-azevedocommented, Nov 8, 2019

Hey @andrew-oreshko , I also encountered this issue. For now, and as a temporary fix, I created my own withTheme HOC using the code provided by @zepatrik. Beware that this will only work if your project uses react version 16.3.0 or higher.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get ref from connected redux component withStyles
In TodoList , I'm using withStyles to add a blue border around the todo list to show that withStyles is working, and I'm...
Read more >
Forwarding Refs - React
Ref forwarding is not limited to DOM components. You can forward refs to class component instances, too. Note for component library maintainers. When...
Read more >
forward-ref and theme from styled wrapper - CodeSandbox
forward -ref and theme from styled wrapper. Copied source from https://github.com/styled-components/styled-components#basic.
Read more >
styled() - MUI System
It uses MUI's default theme if no theme is available in React context. ... bool [optional]): Indicates whether the prop should be forwarded...
Read more >
React Ts Forward Ref With Styled Components - StackBlitz
import React, { useRef, useEffect, useState }. from "react";. import { render } from "react-dom";. import { Foo } from "./foo";. import "./style.css";....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found