Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. render() {
  2. const func = (node) => {
  3. React.Children.forEach(node.children, (childNode) => {
  4. if (childNode && childNode.props && typeof childNode.props.children === "object") {
  5. func(childNode.props);
  6. }
  7. else if (typeof childNode.props.children === "string"){
  8. console.log(childNode.props);
  9. }
  10. })
  11. }
  12.  
  13. func(this.props);
  14.  
  15. return <span>{ this.props.children }</span>;
  16. }
  17.  
  18. class Hello extends React.Component {
  19.  
  20. constructor() {
  21. super();
  22.  
  23. this.state = {
  24. children: [
  25. <div>test</div>,
  26. <div>test2</div>
  27. ]
  28. };
  29.  
  30. setTimeout(() => {
  31. this.setState({
  32. children: [
  33. <div>new!</div>,
  34. <div>changed!</div>
  35. ]
  36. });
  37. }, 2000);
  38.  
  39. }
  40.  
  41. render() {
  42. return <p>{this.state.children}</p>;
  43. }
  44.  
  45. }
  46.  
  47. ReactDOM.render(
  48. <Hello />,
  49. document.getElementById('container')
  50. );
  51.  
  52. render() {
  53. const func = (children) => {
  54. React.Children.map(children, (childNode) => {
  55. if (typeof childNode.props.children === "string")
  56. return React.cloneElement(childNode, childNode.props, "something");
  57. return React.cloneElement(childNode, childNode.props, func(childNode.props.children));
  58. })
  59. }
  60.  
  61. return <span>{ func(this.props.children) }</span>;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement