Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
191
rated 0 times [  195] [ 4]  / answers: 1 / hits: 52186  / 9 Years ago, thu, september 17, 2015, 12:00:00

I'd like to have a condition that states if a prop is a React node then just place it as a child within a component, and if it's not, take some action to make it into a component.
This way my component will be able to accept this prop as an array of strings, or an array of nodes.


I tried to check if React.PropTypes.node would return a boolean but it doesn't work.


Say I have a module called List and there's a prop called items. I'd like to be able to pass


var items = [
"One",
"Two",
"Three"
]

as well as


var items = function () {
return (
<li>One</li>
<li>Two</li>
<li>Three</li>
)
}

And within the component have some logic that would detect the difference and if it's a plain array (not an array of nodes) be able to map the items.


More From » properties

 Answers
56

React has a function just to check if a variable is an element, here's the docs.


React.isValidElement(obj)

[#65025] Wednesday, September 16, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
domeniccolti

Total Points: 276
Total Questions: 98
Total Answers: 93

Location: India
Member since Fri, May 13, 2022
2 Years ago
domeniccolti questions
Mon, Oct 18, 21, 00:00, 3 Years ago
Thu, Oct 14, 21, 00:00, 3 Years ago
Thu, Jul 15, 21, 00:00, 3 Years ago
Sat, Oct 24, 20, 00:00, 4 Years ago
Thu, Sep 3, 20, 00:00, 4 Years ago
;