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.

PropType for element prop breaks when dealing with custom components

See original GitHub issue

Problem

The element prop has a required PropType of string, which is a problem when trying to use custom components.

Specifically, the element prop is used to create the base element for the InfiniteScroll component in the following way:

React.createElement(element, props, children)

As can be seen in the createElement docs, the argument element can have several types. These include string, or any React element.

However, when element is a string, React expects that string to be an HTML tag name like div or span, and will break completely if that isn’t the case. For custom components (e.g., Grid), the argument must be of type React element.

So, HTML tags will work with:

<InfiniteScroll element="div" />

But there’s no good way to do a custom component. If I try to pass the component name as a string:

<InfiniteScroll element="Grid" />

createElement will try to find a matching HTML tag and fail completely when none exists.

On the other hand, if I pass the component as an element:

<InfiniteScroll element={Grid} />

Everything will render properly, but the console will show an error for failed prop types (since Grid is not a string).

Suggested Fix

I think this is as simple as changing the PropType for element to: PropTypes.oneOfType([PropTypes.string, PropTypes.element]).

I’d be more than happy to submit a PR if this is a good idea.

Issue Analytics

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

github_iconTop GitHub Comments

6reactions
cspicuzzacommented, Aug 15, 2018

I’m having this same issue when using styled-components

declared element:

export const TableScroll = styled.div`
  display: table;
  table-layout: fixed;
  border-collapse: collapse;
  width: 100%;
  box-sizing: border-box;
  will-change: transform;
  backface-visibility: hidden;
  -webkit-overflow-scrolling: touch;
`

Infinite Scroll

<InfiniteScroll
   element={TableScroll}
   initialLoad={false}
   pageStart={1}
   loadMore={this.loadMore}
   hasMore={this.hasMore()}
   threshold={500}
   useWindow={true}
   loader={<AjaxLoader key={`page-${inventory.page}`} />}
    >

The data renders into the element, but throws the same Failed Prop Type warning and InfiniteScroll doesn’t work. Using a standard element like ‘div’ works fine.

1reaction
PerlBugcommented, Feb 12, 2019

Same issue here

Read more comments on GitHub >

github_iconTop Results From Across the Web

PropType for element prop breaks when dealing with custom ...
Problem The element prop has a required PropType of string, which is a problem when trying to use custom components.
Read more >
Master React PropTypes w/ Comprehensive Guide - Copycat
Master React PropTypes With This Comprehensive Guide ... In React, we build applications by breaking down the UI into several components.
Read more >
Typechecking With PropTypes - React
To run typechecking on the props for a component, you can assign the special propTypes property: import PropTypes from 'prop-types'; class Greeting extends ......
Read more >
How To Customize React Components with Props
In this tutorial, you'll create custom components by passing props to your component. Props are arguments that you provide to a JSX element....
Read more >
Should I define standard React PropTypes on my component?
After doing some testing it seems like the answer is no, most of the native properties on HTML elements do not have type-checking...
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