Building a Simple Todo List App in React

thank you @senocular, I definitely have a lot more to learn!

That’s a good question :stuck_out_tongue:

The easiest approach would be to set the contenteditable attribute on the element when you click. After making the change, you update the state with the new value.

You can read more about this attribute here:

Does this help? I can try to put together an example, but I am a bit swamped this week.


I’m trying to edit it in the same input that it was submitted.
I already am able when i click on it to see its value in the input field. But when i press add, it adds new item instead of updating the old one. That’s where i’m stuck.

If you look the code, i guess that i need a way in addColor to check if it’s key exists, to update that item. Any thoughts on that?

addColor(c) {
    // check for valid hex code with regex
if (this.state.color.match(/(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i)) {
  console.log('Hex ok');
  var newColor = {
    text: this.state.color,

  this.setState((prevState) => {
    return {
      colors: prevState.colors.concat(newColor)
} else {
  console.log('Hex NOT ok');

  color: ""



onChangeEdit(value) {
  color: value

input field:

<input value={this.state.color}
            onChange={e => this.onChangeEdit(}
            name="color" />

Hello Kirupa,
I am learning React following your book. It is really good.
I have a question regarding the ToDoList example. You have suggested two ways of adding new items to the array (one using unshift, and the one using concat).
When I type the code you have listed above, it works well. However, if I try to change the concat() method on the array “items” to “unshift” I start getting this error:

please send me edit part of this todo

Sir please send me code to edit the elements
my emai id is [email protected]
thank you

Great job on the tutorial ! Learned a lot. It’s a good starter project for react beginners like me :slightly_smiling_face:

1 Like

Welcome to the forums @Parikhit_Sarkar! Glad you found the tutorial helpful :slight_smile:

I have a small question sir. When I use the demo I was able to see all newly added items display at the top of the todo list, So I watched your video to figure it out, how to write the coding for it. In the video, when you run the app, newly added items move to the end not the top. So can you please let mw know how to display newly added items at the top of the to do list.

Just speculating, but there’s a line that looks like this in addItem:


I think you can just swap it around like so:


It works but my todo list is not sorted. Just think like this,
I added A, B, C to my list. In my todo list it shows like this B, A, C(Sometimes A,B,C too but the items I added before is getting changed). how ever the newly added one comes to the up of the list. can you please solve this too?

now everything is working superbly Thanks a lot!!!

If I want to do something like below what should I do,
I want to add a button for the listed items called completed items and I want to bring a listed item to the end of the list when I click that button! So what is the code for it?

Hello friends,

I would really appreciate it if someone could help me find the bug here.

I’ve followed up to the end of the “adding items” section. Somehow the items array is behind by 1 item according to what is being printed.

For example if I entered “abc”, it prints an empty array. If I then enter “def”, it prints an array with just “abc”. Why is this?

Here’s my TodoList.js:

import React, { Component } from "react";
class TodoList extends Component {
  constructor(props) {

    this.state = {
      items: []

    this.addItem = this.addItem.bind(this);

  addItem(e) {
    if (this._inputElement.value !== "") {
      var newItem = {
        text: this._inputElement.value,
      this.setState((prevState) => {
        return { 
          items: prevState.items.concat(newItem) 
      this._inputElement.value = "";
  render() {
    return (
      <div className="todoListMain">
        <div className="header">
          <form onSubmit={this.addItem}>
            {/* this thing here with the ref
            is so that we can access the input element via the
            reference _inputElement
            <input ref={(a) => this._inputElement = a}
                  placeholder="enter task">
            <button type="submit">add</button>
export default TodoList;

Thanks in advance.

Amazing tutorials and website by the way! Everyone is so nice on here. Wish I found this sooner :heart_eyes:

When you use setState with a function, it doesn’t immediately set the state. It gets deferred for just a little bit meaning your log() call will happen before the state changes. You can read more about this here (the preview just says but it goes to the correct part in the documentation that talks about this):


Interesting - thank you!

Hello! What if we get input value throughout:

addItem(e) {[0].value?

Then you won’t need to use refs

Hi there Kirupa,

In the tutorial, you wrote “we are going to tie together a lot of the concepts and techniques you’ve learned”. Can you specify which tutorials you strongly recommend a newbie to React go through before attempting the simple to-do list app?

Thank you kindly,


If this is your first jump into React, I would encourage starting at the top itself and going down: The concepts build on top of each other.

If you are already familiar with React, it would be a fairly quick review. You can go through the TodoList tutorial and see what you need more details on and focus on those.

Kirupa :smile:

1 Like