How to declare PropTypes for recursive data structures like a tree
See original GitHub issueHi,
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:
- Created 8 years ago
- Comments:15 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think this works
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.