React.Children.map recursively?

后端 未结 5 1835
青春惊慌失措
青春惊慌失措 2020-12-13 19:53

I\'m building a React Component for rendering an HTML form and I\'m finding the need to recursively iterate through all children of my parent Form component in order to add

5条回答
  •  [愿得一人]
    2020-12-13 20:21

    This thread actually misses the right answer:

    const mapRecursive = (children, callback) => (
      React.Children.map(
        children,
        child => (
          child.props.children
            ? [callback(child), mapRecursive(child.props.children, callback)]
            : callback(child)
        ),
      )
    );
    

提交回复
热议问题