-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from rahulakrishna/recursion-in-react
recursion in react
- Loading branch information
Showing
14 changed files
with
275 additions
and
464 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,14 @@ | ||
.DS_Store | ||
.jekyll-cache/* | ||
_site/ | ||
.sass-cache/ | ||
.jekyll-cache/ | ||
.jekyll-metadata | ||
# Ignore folders generated by Bundler | ||
.bundle/ | ||
vendor/./.DS_Store | ||
./_assets/.DS_Store | ||
./_includes/.DS_Store | ||
./_layouts/.DS_Store | ||
./images/.DS_Store | ||
./images/budgeting-tool/.DS_Store | ||
./images/recursion-in-react/.DS_Store |
230 changes: 0 additions & 230 deletions
230
...l--Converters--Markdown/0e/42ece69f0fcb8d73a940a10216bb97d1cad9f83c579759ec9f3094d845815f
This file was deleted.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
--- | ||
layout: post | ||
title: "Recursion in React" | ||
author: "Rahul Krishna" | ||
date: 2024-02-21 11:15:00 +0530 | ||
categories: [notes] | ||
tags: [javascript, react] | ||
--- | ||
|
||
### TLDR; | ||
|
||
--- | ||
|
||
TIL that you can have recursive components in React! | ||
|
||
<aside> | ||
💡 React uses one-way data flow, passing data down the component hierarchy from parent to child component | ||
</aside> | ||
<br/> | ||
So while thinking in React, it may not immediately occur that you can call a **parent** inside the **child!** But it is possible | ||
|
||
### Consider a Data Structure like this: | ||
|
||
--- | ||
|
||
```jsx | ||
const comments = [ | ||
{ | ||
content: "Comment 1", | ||
level: 0, | ||
comments: [ | ||
{ | ||
content: "Comment 1 > 1", | ||
level: 1, | ||
comments: [ | ||
{ | ||
content: "Comment 1 > 1 > 1", | ||
level: 2, | ||
comments: [], | ||
}, | ||
], | ||
}, | ||
{ | ||
content: "Comment 1 > 2", | ||
level: 1, | ||
comments: [], | ||
}, | ||
], | ||
}, | ||
{ | ||
content: "Comment 2", | ||
level: 0, | ||
comments: [], | ||
}, | ||
]; | ||
``` | ||
|
||
### Doing this in Javascript | ||
|
||
--- | ||
|
||
```jsx | ||
function displayComments(comments) { | ||
return comments.reduce((acc, comment) => { | ||
const prefix = Array(comment.level) | ||
.fill("|") | ||
.reduce((acc, item) => `${acc} ${item}`, ""); | ||
|
||
const moreComments = displayComments(comment.comments); | ||
|
||
const stringToReturn = `${prefix} ${comment.content} \n ${moreComments}`; | ||
return `${acc} ${stringToReturn}`; | ||
}, ""); | ||
} | ||
``` | ||
|
||
Result: | ||
|
||
![Javascript Result](/images/recursion-in-react/javascript-result.png) | ||
|
||
### Doing this in React | ||
|
||
--- | ||
|
||
```jsx | ||
function Comment({ comment }) { | ||
const prefix = Array(comment.level) | ||
.fill("|") | ||
.reduce((acc, item) => `${acc} ${item}`, ""); | ||
return ( | ||
<div> | ||
{prefix} {comment.content} | ||
<Comments comments={comment.comments} /> | ||
</div> | ||
); | ||
} | ||
|
||
export default function Comments({ comments }) { | ||
return comments.map((comment) => { | ||
return <Comment comment={comment} />; | ||
}); | ||
} | ||
``` | ||
|
||
Result: | ||
|
||
![React Result](/images/recursion-in-react/react-result.png) | ||
|
||
### Actual example of rendering a comment tree | ||
|
||
--- | ||
|
||
It’s unlikely we’ll be using `|` prefixes when actually building a comment tree. We’ll more likely be playing around with `padding` and `margin`. | ||
|
||
Here’s a code example of actual comment tree rendering in React with TailwindCSS: [https://github.com/rahulakrishna/hackrmn/blob/master/src/app/[storylist]/comments/[commentid]/comment-block.js](https://github.com/rahulakrishna/hackrmn/blob/master/src/app/%5Bstorylist%5D/comments/%5Bcommentid%5D/comment-block.js) | ||
|
||
![Untitled](/images/recursion-in-react/real-life-example.png) | ||
|
||
### References | ||
|
||
--- | ||
|
||
- Thinking in React: [https://react.dev/learn/thinking-in-react](https://react.dev/learn/thinking-in-react) | ||
- hackrmn repo: [https://github.com/rahulakrishna/hackrmn/tree/master](https://github.com/rahulakrishna/hackrmn/tree/master) |
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.