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.

How to declare PropTypes for recursive data structures like a tree

See original GitHub issue

Hi,

I’d like to do something like:

var Node = PT.shape({
    value: ...,
    childrens: PT.arrayOf(Node).isRequired,
});

As you can guess this does not really work because it ends up calling PT.arrayOf(undefined).isRequired

I know this is probably a javascript limitation due do not implementing lazy evaluation, but is there a way to declare such structure properly in PropTypes? I’ve not seen this documented.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:15 (4 by maintainers)

github_iconTop GitHub Comments

51reactions
jethrolarsoncommented, Mar 16, 2017

I think this works

var nodeShape = {
    value: ...
};
nodeShape.children = PT.arrayOf(PT.shape(nodeShape)).isRequired;

var Node = PT.shape(nodeShape);
27reactions
gaearoncommented, Mar 27, 2016

This doesn’t seem like a very common use case, and can be arbitrarily slow so I don’t think it’s something we want to support in the core. I’m glad you found a solution that works for you! I’m closing but let us know if you have more thoughts on this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can a React prop type be defined recursively? - Stack Overflow
the propTypes for Tree can be defined as: import PropTypes from 'prop-types'; const treeDataShape = { nodeName: ...
Read more >
Defining Recursive PropTypes. Using simple vanilla JS
How to define a recursive React propType that references itself via simple JS, especially useful for tree-shaped data structures.
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 >
Trees and Recursion | Recursive Data Structures | Geekific
Over the past few months, we managed to cover several Tree implementations, and if you take a closer look at the methods within...
Read more >
[Solved]-Can a React prop type be defined recursively?-Reactjs
I created recursive prop types something like this and it worked for me. Let me know if it works for ... that takes...
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