Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
TODOLIST Update in REACT

So I was trying to update the value I got by the Addlist and I tried this but this isn;t working. Also when I click on the '+' button without writing anything, an empty list is created. How should I stop it. I've attached a code below.

import React from "react";
import "./App.css";
import { useState } from "react";
import TodoList from "./components/TodoList";

function App() {
  const [input, setInput] = useState("");
  const [list, setList] = useState([]);
  

  const updateList = (e) => {
    
    setInput(e.target.value);
    
  };

  const AddList = () => {
    console.log("value added")
    setList((addValue) => {
      
        
        return [...addValue, input];
      
     
    });
    setInput("");
  };

  const updateItems=(id)=>{
     const newValue=[...list].map((newVal)=>{
       if(input.id===id){
         input.text='';
       }
       return newVal;
     })
     setList(newValue);
    
  }

  const deleteItems = (id) => {
    console.log("deleted");
    setList((addValue) => {
      return addValue.filter((element, index) => {
        return index !== id;
      });
    });
  };

  return (
    
    <div className="todo-app">
      <h1> Enter Anything</h1>
      <input
        type="text"
        placeholder="Add anything"
        value={input}
        onChange={updateList}
      />
      <button onClick={AddList}>+</button>
      <ul>
        {list.map((itemsvalue, id) => {
          return (
            <TodoList
              itemsValue={itemsvalue}
              key={id}
              onSelect={deleteItems}
              id={id}
               onUpdate={updateItems}
            />
          );
        })}
      </ul>
    </div>
  );
}

export default App;

Any kind of help would be appreciated. Also if I want to split this into multiple components is there a way to do.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

When user clicks on the add button there is the check for empty String AddList method for ex:- User updates second index value, second position value will get updated.

const [input, setInput] = useState('');
const [list, setList] = useState([]);
const [index, setIndex] = useState(null);

const updateList = (e) => {
    setInput(e.target.value);
};

useEffect(() => {
    setList(list);
    console.log(list, '<>?');
}, [index]);

const AddList = () => {
    if (input.trim() !== '') {
        setList([...list, input]);
    }
    setInput('');
};

const updateValue = (index) => {
    console.log(list[index]);
    setIndex(index);
    if (list[index].trim() !== '') {
        setInput(list[index]);
    }
};

const UpdateList = () => {
    list[index] = input;
    console.log(list, 'before  <>?');
    setIndex(null);
    setInput('');
};

return (
    <div>
        <input type="text" placeholder="Add anything" value={input} onChange={updateList} />
        <button disabled={!index && !list.length === 0} onClick={AddList}>
            Add
        </button>
        <button disabled={input.trim() === ''} onClick={UpdateList}>
            Update
        </button>
        {list.map((m, index) => (
            <h1 style={{ border: '1px solid black' }} onClick={() => updateValue(index)}>
                {m}
            </h1>
        ))}
    </div>
);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!