Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

144
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda