hi
- User
hi
- Assistant
Hello! How can I assist you today?
- User
create a simple react blog app with create, edit and delete function and routing
- Assistant
Creating a simple React blog app with basic CRUD (Create, Read, Update, Delete) operations and routing involves multiple steps. I'll guide you through setting up a basic React application, adding routing capabilities, and implementing CRUD features. Since this walkthrough aims for simplicity, we'll focus on front-end operations without a back-end or database. Data will only persist as long as the app is running.
Step 1: Setting Up Your React App
First, ensure you have Node.js and npm (Node Package Manager) installed. Then, create a new React application:
npx create-react-app react-blog cd react-blogStep 2: Install React Router
To handle routing, we'll use React Router:
npm install react-router-dom@6Step 3: Create Basic Components
-
Home Page (to list all blog posts)
Create a new file
HomePage.jsin thesrcdirectory.// src/HomePage.js import React from 'react'; function HomePage({ posts, onDelete }) { return ( <div> <h1>Blog Posts</h1> {posts.map((post) => ( <div key={post.id}> <h2>{post.title}</h2> <p>{post.content}</p> <button onClick={() => onDelete(post.id)}>Delete</button> </div> ))} </div> ); } export default HomePage; -
Add/Edit Post Page
Create a new file
PostForm.jsin thesrcdirectory.// src/PostForm.js import React, { useState, useEffect } from 'react'; import { useNavigate, useParams } from 'react-router-dom'; function PostForm({ posts, setPosts }) { const { postId } = useParams(); const navigate = useNavigate(); const isEdit = postId !== "new"; const [post, setPost] = useState({ title: '', content: '' }); useEffect(() => { if (isEdit) { const existingPost = posts.find((p) => p.id === parseInt(postId, 10)); if (existingPost) setPost(existingPost); } }, [postId, isEdit, posts]); const handleSubmit = (e) => { e.preventDefault(); if (isEdit) { const updatedPosts = posts.map((p) => (p.id === parseInt(postId, 10) ? post : p)); setPosts(updatedPosts); } else { const newPost = { ...post, id: Date.now() }; setPosts([...posts, newPost]); } navigate('/'); }; return ( <form onSubmit={handleSubmit}> <h1>{isEdit ? "Edit Post" : "Add a New Post"}</h1> <label> Title: <input type="text" value={post.title} onChange={(e) => setPost({ ...post, title: e.target.value })} required /> </label> <br /> <label> Content: <textarea value={post.content} onChange={(e) => setPost({ ...post, content: e.target.value })} required ></textarea> </label> <br /> <button type="submit">Save</button> </form> ); } export default PostForm;
Step 4: Setup Routing
Modify
App.jsto set up routing:// src/App.js import React, { useState } from 'react'; import { BrowserRouter as Router, Routes, Route, Link } from 'react-router-dom'; import HomePage from './HomePage'; import PostForm from './PostForm'; function App() { const [posts, setPosts] = useState([]); const handleDelete = (id) => { const updatedPosts = posts.filter((post) => post.id !== id); setPosts(updatedPosts); }; return ( <Router> <nav> <Link to="/">Home</Link> | <Link to="/post/new">New Post</Link> </nav> <Routes> <Route path="/" element={<HomePage posts={posts} onDelete={handleDelete} />} /> <Route path="/post/:postId" element={<PostForm posts={posts} setPosts={setPosts} />} /> </Routes> </Router> ); } export default App;Final Step: Run Your Application
Finally, you can start your application and test the CRUD operations:
npm startYour simple React blog app is now set up with create, edit, and delete functionalities, along with basic routing. Remember, this example stores data in the component's state, so if the page is reloaded, all changes will be lost. For a real-world application, you would typically integrate a back-end and database to persist data.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- gpt-3.5-turbo-0301Compare side by side →
- gpt-4-0314Compare side by side →
- gpt-3.5-turbo-0301Compare side by side →
- gpt-3.5-turbo-0301Compare side by side →
- gpt-4-0314Compare side by side →
- gpt-4-0314Compare side by side →
- gpt-4-0314Compare side by side →
- gpt-3.5-turbo-0301Compare side by side →
- gpt-3.5-turbo-0301Compare side by side →
- gpt-4-0314Compare side by side →
- gpt-4-0314Compare side by side →
- gpt-4-0314Compare side by side →