Tuesday, 15 April 2014

reactjs - How to count the number of Grandchildren -



reactjs - How to count the number of Grandchildren -

new react , jsx apologies if basic question.

i have "grandparent" node , can count number of "parent" nodes using:

{react.children.count(this.props.children)

however - given particular "parent" - how can homecoming count of children? can @ parent level, can't figure out how @ grandparent level.

as usual - best explain example, have illustration at: http://jsfiddle.net/zuoza31g/1/

i output be:

i grandfather 'grandfather' have '1' children have '4' grandchildren father 'father' have '4' children boy 'son 1' boy 'son 2' boy 'son 3' boy 'son 4'

it's part of much bigger problem, i'm simplified downwards illustration ..

can help please?

you can count number of grand children using next code:

var count = 0; react.children.foreach(this.props.children, function (child){ count = count + react.children.count(child.props.children); });

here fiddle solution implemented: http://jsfiddle.net/db8o8920/

reactjs

1 comment: