Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React - Recursive Component #2

Open
201flaviosilva opened this issue Jul 6, 2022 · 0 comments
Open

React - Recursive Component #2

201flaviosilva opened this issue Jul 6, 2022 · 0 comments

Comments

@201flaviosilva
Copy link
Member

Example: https://www.olioapps.com/blog/react-recursion/

import React from "react";

export default function Branch({ title = "title", data }) {
	function renderBranch(key, index) {
		const item = data[key];

		if (Array.isArray(item)) {
			if (item.length > 0) return <Branch title={key} data={item} />;
			return null;

		} else if (typeof item === "object") {
			return <Branch title={key} data={item} />;

		} else {
			return (<p
				key={index}
				style={{ marginLeft: 4px }}
			>
				{key}: <span style={{ fontWeight: "bold" }}>{item}</span>
			</p>
			);
		}
	}

  return (
    <div>
      <h2>{title}</h2>
      <div>
        {
          Object.keys(data).map((key, index) => renderBranch(key, index))
        }
      </div>
    </div>
  );
}
@201flaviosilva 201flaviosilva transferred this issue from 201flaviosilva/utilidades May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant