1

I'm creating a simple to-do app where the user can select the date, write the todo and set from and to time.

I have added 4 fields in my render function. Please guide me if my approach is wrong. at the end I want have an array todolist with index element and subarray index element as date and and children elements have todo, from and to time.

example:

todolist[
    27-06-2018 : [
       todo: eat breakfast
       from: 9:00
       to: 9:30
    ]
    28-06-2018 :[
       todo: eat lunch
       from: 12:00
       to: 12:30
    ]
]

for the above requirement how to initailaize the state?

2 Answers 2

1

I suggest that you store yours todos as objects in your todolist array like:

todolist[
  {
    date: '27-06-2018',
    todo: 'eat beakfast',
    from: '9:00',
    to: '9:30',
  },
  {
    date: '28-06-2018',
    todo: 'eat lunch',
    from: '9:00',
    to: '9:30',
  }
];

and to filter through the array by date you can use the filter method:

todoslist.filter(todo => todo.date == '27-06-2018');
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, can you please tell me how to initialize state for this?
you can initialize your state with an empty array of todos like this this.state = {todos: []} and then to set the todos you do: todos = []; todos.push({insertyourtodoobjecthere}) then this.setState({todos}) as in es6 {todos} is like doing {todos: todos}
0
todolist[
{
    27-06-2018 : [
      { todo: eat breakfast},
       {from: 9:00},
      { to: 9:30}
     ]
    },{
    28-06-2018 :[
       {todo: eat lunch},
      { from: 12:00},
      { to: 12:30}
       ]
    }
]

Try making objects of the items in the array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.