Last active
August 5, 2022 08:15
-
-
Save nkvenom/bf7b1adfe982cb47dee3 to your computer and use it in GitHub Desktop.
React Example Render Recursive Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Recursive Component in React</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.11.0/umd/react.development.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.11.0/umd/react-dom.development.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script> | |
</head> | |
<body> | |
<div id="react-app"></div> | |
<script type="text/babel"> | |
function BookmarkNode({ node }) { | |
if (!node.children) return <a href={node.url}> {node.name} </a>; | |
return ( | |
<div> | |
<strong>{node.name}</strong> | |
<ul> | |
{node.children.map(c => (<li key={c.id}> <BookmarkNode node={c} /> </li>))} | |
</ul> | |
</div> | |
); | |
}; | |
var FAKE_BOOKMARKS = { | |
"id": "1", | |
"name": "Bookmarks bar", | |
"children": [ | |
{ | |
"id": "6", | |
"name": "TensorFlow", | |
"url": "http://www.tensorflow.org/" | |
}, | |
{ | |
"id": "96", | |
"name": "Introduction to Deep Learning with Python", | |
"url": "https://www.youtube.com/watch?v=S75EdAcXHKk" | |
}, | |
{ | |
"children": [ | |
{ | |
"id": "8", | |
"name": "What interests reddit?", | |
"url": "http://markallenthornton.com/blog/what-interests-reddit/?utm_content=buffer6ba72&utm_medium=social&utm_source=twitter.com&utm_campaign=buffer" | |
}, | |
{ | |
"id": "215", | |
"name": "Foobaz", | |
"url": "http://www.code-labs.io/", | |
"children": [ | |
{ | |
"id": "9", | |
"name": "NG2", | |
"url": "https://angular.io" | |
}, | |
{ | |
"id": "11", | |
"name": "Fast Refresh", | |
"url": "https://facebook.github.io/react-native/docs/fast-refresh", | |
"children": [{ | |
"id": 56, | |
"name": "Hot Reloading with Time Travel", | |
"url": "https://www.youtube.com/watch?v=xsSnOQynTHs" | |
}, | |
{ | |
"id": 196, | |
"name": "Gource Visualization", | |
"url": "https://www.youtube.com/watch?v=bzLCvFG6WbY" | |
}] | |
} | |
] | |
} | |
], | |
"id": "7", | |
"name": "Dev", | |
} | |
] | |
}; | |
ReactDOM.render( | |
<BookmarkNode node={FAKE_BOOKMARKS} />, document.getElementById('react-app') | |
); | |
</script> | |
</body> | |
</html> |
Very useful
Hello
I want to add a checkbox on every node and store it in the state array. How can I do this?
I want to create array of ids of the nodes
Hello
I want to add a checkbox on every node and store it in the state array. How can I do this?
I want to create array of ids of the nodes
Take a look at this: https://gist.github.com/nestoralonso/22d85ff16448a375a681a8dd37b5bb88
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, this helped me out. Exact same data structure I was dealing with.