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.

PropsTable is not working with components encapsulated by React.forwardRef()

See original GitHub issue

<PropsTable of={Input} /> only works when I remove the React.forwardRef(). The following code is Typescript, and I also have typescript: true on my config.

import * as React from "react"
import styled from "styled-components"

// Estilos ---------------------------------------------------------------------

const StyledInput = styled.input.attrs({
  className: "form-control",
})`
  height: 40px;
  margin-bottom: 0;
  font-size: 0.8125rem;
`

const FormRow = styled.div.attrs({
  className: "form-group",
})`
  margin-bottom: 1.25rem;
`

// <Input /> -------------------------------------------------------------------

interface IPInput {
  id: string
  label?: string | false
  placeholder?: string
}

const Input = React.forwardRef(
  (
    { id, label, placeholder, ...extraProps }: IPInput,
    ref: React.RefObject<HTMLInputElement>,
  ) => (
    <FormRow>
      <label
        htmlFor={id}
        className={[false === label ? "sr-only" : ""].join(" ")}
      >
        {false === label && placeholder ? placeholder : label}
      </label>
      <StyledInput
        innerRef={ref}
        id={id}
        placeholder={placeholder}
        {...extraProps}
      />
    </FormRow>
  ),
)

export default Input

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
pedronauckcommented, Mar 12, 2019

I think that this will be fixed in the v1 ✌️

2reactions
jnaudincommented, Jan 25, 2019

Hi

PropsTable uses __docgenInfo’s data. you do not have access to it in a HOC but in the wrapped component. You have to give the wrapped component to <PropsTable>

2 solutions :

  • import both component and wrapped component (you have to export wrapped component). Use the wrapped component to display <PropsTable>
// ./MyComponent.js
export const MyComponent = ...;
export default MyHoc(MyComponent);

// ./MyComponent.mdx
import MyComponent, {MyComponent as MyComponentWithPropTypes} from './MyComponent'
<Playground><MyComponent /></Playground>
<PropsTable of={MyComponentWithPropTypes} />
  • use render().type to access to wrapped component via hoc
// ./MyComponent.js
const MyComponent = ...;
export default MyHoc(MyComponent);

// ./MyComponent.mdx
import MyComponent from './MyComponent'
<Playground><MyComponent /></Playground>
<PropsTable of={MyComponent.render().type} />

By default MyComponent.render().type does not have __docgenInfo. __docgenInfo is generated by babel-plugin-react-docgen and the default value of the resolver option (findAllExportedComponentDefinition) doesn’t create the __docgenInfo if the component is not exported. You should use findAllComponentDefinitions value. See https://github.com/storybooks/babel-plugin-react-docgen#babelrc-options for more info.

Otherwise you should export the wrapped component (like in the first solution) to have access to __docgenInfo.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PropsTable is not working with components encapsulated by ...
only works when I remove the React.forwardRef(). The following code is Typescript, and I also have typescript: true on my config. import ...
Read more >
Forwarding Refs - React
Regular function or class components don't receive the ref argument, and ref is not available in props either. Ref forwarding is not limited...
Read more >
React - sending down ref as a prop not working - Stack Overflow
Working example with ref renamed to parentRef in both components. ... forwardRef to pass refs in the component hierarchy.
Read more >
Everything You Need to Know About Refs in React
Short for “reference”, refs are a way to access underlying DOM elements in a React component. There are many reasons why you would...
Read more >
Forwarding refs to components - Mario Kandut
forwardRef API receives props and ref parameters and returns a React.node . In HOC, it's important to know that ref is not a...
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