Creating a family tree website in Next.js involves several steps: setting up the Next.js project, designing a data structure for family members, creating components to display the tree, and optionally adding a backend or local storage to save the data.
Below is a simple example to get you started. This example will:
- Use Next.js with React.
- Store family members in a tree-like structure in React state.
- Display the family tree recursively.
- Allow adding new family members.
Step 1: Setup Next.js Project
If you haven't already, create a new Next.js app:
npx create-next-app@latest family-tree
cd family-tree
npm run dev
Step 2: Define Family Member Data Structure
We will use a simple tree structure where each member has:
id
name
children (array of family members)
Step 3: Create Components
Create a component to display a family member and their children recursively.
Step 4: Implement the Family Tree Page
Create a page at pages/index.js:
import { useState } from "react";
const initialFamily = {
id: 1,
name: "Grandparent",
children: [
{
id: 2,
name: "Parent 1",
children: [
{ id: 4, name: "Child 1", children: [] },
{ id: 5, name: "Child 2", children: [] },
],
},
{
id: 3,
name: "Parent 2",
children: [],
},
],
};
function FamilyMember({ member, addChild }) {
const [showAdd, setShowAdd] = useState(false);
const [childName, setChildName] = useState("");
return (
<div style={{ marginLeft: 20, borderLeft: "1px solid #ccc", paddingLeft: 10 }}>
<div>
<strong>{member.name}</strong>{" "}
<button onClick={() => setShowAdd(!showAdd)}>
{showAdd ? "Cancel" : "Add Child"}
</button>
</div>
{showAdd && (
<div style={{ marginTop: 5 }}>
<input
type="text"
placeholder="Child name"
value={childName}
onChange={(e) => setChildName(e.target.value)}
/>
<button
onClick={() => {
if (childName.trim()) {
addChild(member.id, childName.trim());
setChildName("");
setShowAdd(false);
}
}}
>
Add
</button>
</div>
)}
<div>
{member.children.map((child) => (
<FamilyMember key={child.id} member={child} addChild={addChild} />
))}
</div>
</div>
);
}
export default function Home() {
const [family, setFamily] = useState(initialFamily);
const [nextId, setNextId] = useState(6);
// Recursive function to add child to the right member
function addChild(parentId, childName) {
function addChildRec(node) {
if (node.id === parentId) {
return {
...node,
children: [...node.children, { id: nextId, name: childName, children: [] }],
};
}
return {
...node,
children: node.children.map(addChildRec),
};
}
setFamily(addChildRec(family));
setNextId(nextId + 1);
}
return (
<div style={{ padding: 20 }}>
<h1>Family Tree</h1>
<FamilyMember member={family} addChild={addChild} />
</div>
);
}
How it works:
- The family tree is stored in React state.
- The
FamilyMember component displays a member and recursively renders children.
- You can add children to any member.
- The tree updates dynamically.
Next Steps / Improvements
- Persist data in localStorage or a backend.
- Add editing and deleting members.
- Improve UI with CSS or a library like Tailwind CSS.
- Visualize the tree better with a graph library (e.g.,
react-d3-tree).
If you want, I can help you extend this with persistence or a better UI! Would you like that?