🕵️ SicherheitslückenHak5: Hackers Just Poisoned the Rust Supply Chain | Threat Wire(01.09.2026 um 14:00 Uhr)
🕵️ SicherheitslückenHak5: Hackers Found a Way Into Humanoid Robots | Threat Wire(04.09.2026 um 15:04 Uhr)
🔧 AI Nachrichten Bits und so #1021 (Passwort für Laufwerk)(31.08.2026 um 22:15 Uhr)
🔧 AI Nachrichten Bits und so #1022 (Wie Weißbier)(06.09.2026 um 20:39 Uhr)
🍏 iOS / Mac OSHue-App 6.0 ist da: das sind die Neuerungen(07.09.2026 um 17:21 Uhr)
🕵️ SicherheitslückenHak5: Hackers Just Poisoned the Rust Supply Chain | Threat Wire(01.09.2026 um 14:00 Uhr)
🕵️ SicherheitslückenHak5: Hackers Found a Way Into Humanoid Robots | Threat Wire(04.09.2026 um 15:04 Uhr)
🔧 AI Nachrichten Bits und so #1021 (Passwort für Laufwerk)(31.08.2026 um 22:15 Uhr)
🔧 AI Nachrichten Bits und so #1022 (Wie Weißbier)(06.09.2026 um 20:39 Uhr)
🍏 iOS / Mac OSHue-App 6.0 ist da: das sind die Neuerungen(07.09.2026 um 17:21 Uhr)

🔧 Programmierung 🕛 kürzlich 3 Min Lesezeit
0

Simple CRUD using React useState( ) hook.

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht




This is done using React typeScript




  • Here I use three useState to make the operation.

  • I showed the refctor version where I use only one useState hook






Three useState ()






CODE
import React, { useState } from 'react';

// Define the type for a Todo
interface Todo {
id: number;
title: string;
}

const defaultTodos=[{
id:123456789,
title:"Bappa Saha Bapi"
},
{
id:12345678900,
title:"Partho Saha Bapi"
}]

const BasicTodo= () => {

const [todos, setTodos] = useState<Todo[]>(defaultTodos);
const [title, setTitle] = useState<string>('');
const [editId, setEditId] = useState<number | null>(null);

// Function to add or update a todo
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
if (editId !== null) {
// Update existing todo
setTodos(todos.map(todo => (todo.id === editId ? { ...todo, title } : todo)));
setEditId(null);
} else {
// Add new todo
const newTodo: Todo = { id: Date.now(), title };
setTodos([...todos, newTodo]);
}
setTitle('');
};

// Function to delete a todo
const handleDelete = (id: number) => {
setTodos(todos.filter(todo => todo.id !== id));
};

// Function to edit a todo
const handleEdit = (todo: Todo) => {
setTitle(todo.title);
setEditId(todo.id);
};

return (
<div style={{ padding: '20px' }}>
<h1>Todo List</h1>
<form onSubmit={handleSubmit}>
<input
type="text"
value={title}
onChange={(e) => setTitle(e.target.value)}
placeholder="Enter todo title"
required
/>
<button type="submit">{editId !== null ? 'Update' : 'Add'}</button>
</form>
<ul>
{todos.map(todo => (
<li key={todo.id}>
{todo.title} {" "}
<button onClick={() => handleEdit(todo) style={{padding:'5px 20px',margin:'10px'}}}>Edit</button>
<button onClick={() => handleDelete(todo.id)} style={{padding:'5px 20px'}}>Delete</button>
</li>
))}
</ul>
</div>
);
};

export default BasicTodo;









🔥 Refactor One useState ()






CODE
import React, { useState } from 'react';

const App = () => {
// State to hold todos and input fields in one object
const [state, setState] = useState({
todos: [],
title: '',
editId: null,
});

// Function to add or update a todo
const handleSubmit = (e) => {
e.preventDefault();
const { todos, title, editId } = state;

if (editId !== null) {
// Update existing todo
setState({
...state,
todos: todos.map(todo => (todo.id === editId ? { ...todo, title } : todo)),
title: '',
editId: null,
});
} else {
// Add new todo
const newTodo = { id: Date.now(), title };
setState({
...state,
todos: [...todos, newTodo],
title: '',
});
}
};

// Function to delete a todo
const handleDelete = (id) => {
setState({
...state,
todos: state.todos.filter(todo => todo.id !== id),
});
};

// Function to edit a todo
const handleEdit = (todo) => {
setState({
...state,
title: todo.title,
editId: todo.id,
});
};

return (
<div style={{ padding: '20px' }}>
<h1>Todo List</h1>
<form onSubmit={handleSubmit}>
<input
type="text"
value={state.title}
onChange={(e) => setState({ ...state, title: e.target.value })}
placeholder="Enter todo title"
required
/>
<button type="submit">{state.editId !== null ? 'Update' : 'Add'}</button>
</form>
<ul>
{state.todos.map(todo => (
<li key={todo.id}>
{todo.title}
<button onClick={() => handleEdit(todo)}>Edit</button>
<button onClick={() => handleDelete(todo.id)}>Delete</button>
</li>
))}
</ul>
</div>
);
};

export default App;


Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
Hackers Just Poisoned the Rust Supply Chain | Threat Wire
1 Quelle
Hackers Found a Way Into Humanoid Robots | Threat Wire
1 Quelle
Bits und so #1021 (Passwort für Laufwerk)
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Simple CRUD using React useState( ) hook.

Thematisch verwandte Begriffe: Simple, CRUD, using, React · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...