0

I'm trying to delete a data in array using the filter function which return a new array. The problem is how do I push the updated version of the array to the original version?, or if I can't do that, how do I print only the updated version?

here is my state:

export class App extends React.Component {
  constructor() {
    super();
    this.state = {
      todos: [
        id: 1, nama: 'belajar', status: 'belum selesai',
        id: 2, nama: 'kuliah', status: 'belum selesai',
        id: 3, nama: 'sekolah', status: 'belum selesai',
        id: 4, nama: 'makan', status: 'belum selesai'
      ]
    };
    this.state = { value: '' };
    this.state = { isReady: false };
    this.sayHello = this.sayHello.bind(this);
    this.teken = this.teken.bind(this);
    this.done = this.done.bind(this);
  }
}

here is my code:

done(event) {
  this.setState({ isReady: true });
  var str = event.target.value;
  var arr = str.split();
  console.log(this.state.todos);
  const list = todos.filter((todos) => todos.nama !== event.target.value);
  console.log(list);
  this.setState({ todos: list });
  this.setState({ nama: event.target.value });
  todos.push({
    id: event.target.name,
    nama: event.target.value,
    status: 'selesai'
  });
  const find = todos.hasOwnProperty(event);
  if (find) {
    this.setState({ stat: find });
  } else {
    this.setState({ stat: find });
  }
  event.preventDefault();
}

and here is how I print my array

<ul className='list-group list-group-flush'>
  {todos.map((todos) => {
    if (todos.status === 'belum selesai')
      return (
        <li className='list-group-item'>
          {todos.id} {todos.nama}
          <button
            value={todos.nama}
            name={todos.id}
            className='btn form-control form-control-sm col-sm-4 bg-light rounded-pill'
            onClick={this.done}
          >
            Done {todos.id}
          </button>
        </li>
      );
    else
      return (
        <li className='list-group-item'>
          <s>
            {todos.id} {todos.nama}
          </s>
        </li>
      );
  })}
</ul>
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Walls
  • 149
  • 13
  • 1
    Does this answer your question? [Update one of the objects in array, in an immutable way](https://stackoverflow.com/questions/43792457/update-one-of-the-objects-in-array-in-an-immutable-way) – Martin May 20 '20 at 07:28
  • 2
    You need to put the `todos` array into state. That's the entire point of having a state, so that it re-renders when the state changes. – Guy Incognito May 20 '20 at 07:29
  • how do i change my array into a state? do i just copy my array and the move it to the constructor ? – Walls May 20 '20 at 07:37

1 Answers1

1

You are very close. In order to update the list item HTML elements in your component you need to update the list of todos in your state.

done(event) {
  // Copy to a new variable. 
  const nextTodos = this.state.todos.slice();

  // Modify however you want.
  nextTodos.push({ nama: 'new item' });

  // Update the todos. You were missing this part!
  this.setState({ todos: nextTodos });
}

In your render function reference this.state.todos like you are doing now.

In the component constructor, set the initial state.

constructor(props) {
  super(props);
  this.state = {
    todos: [
      // initial todo data
    ],
  };
}
AJcodez
  • 31,780
  • 20
  • 84
  • 118
  • i tried this before, but the problem is my `todos` is not a state, so i think i need to change my array into a state to use this method – Walls May 20 '20 at 07:36
  • @LuthfiMusafa yes it needs to be stored in `state` object if you are planning to change the value and have your component automatically update the HTML elements. – AJcodez May 20 '20 at 07:39
  • how do i change my array into a state ? do i just put it inside the constructor ? – Walls May 20 '20 at 07:45
  • @LuthfiMusafa yes include the todos in the initial state of the component. See the updated answer. – AJcodez May 20 '20 at 07:52
  • i got an error that says `, expected` check my updated question – Walls May 20 '20 at 07:55
  • 1
    @LuthfiMusafa you're missing `{` and `}` around your data items. – AJcodez May 20 '20 at 08:00
  • 1
    It could be worth going through a JavaScript language tutorial. It seems like you are missing prior knowledge required to use React correctly. https://javascript.info/ – AJcodez May 20 '20 at 08:01
  • i still cant get the filtered state of it, can you check my code one last time ? – Walls May 20 '20 at 08:07
  • @LuthfiMusafa you are setting the state and then overwriting the state on the next line. – AJcodez May 21 '20 at 03:12