简体   繁体   English

如何将项目添加到待办事项列表

[英]How to add an item to the todo list

This is a short cut of my addItem function in my todo app, i don't really understand what slice does in this value: this.state.newItem.slice() .这是我的待办事项应用程序中我的 addItem function 的快捷方式,我真的不明白slice在这个值中的作用: this.state.newItem.slice() Can anybody explain me?有人可以解释一下吗? Thank you so much!太感谢了!

constructor(props) {
    super(props);
    this.state = {
      newItem: "",
      list: []
    };

addItem() {
    // create a new item with unique id
    const newItem = {
      id: 1 + Math.random(),
  

    **value: this.state.newItem.slice()**

 
    };

    // copy current list of items
    const list = [...this.state.list];

    // add the new item to the list
    list.push(newItem);

    // update state with new list, reset the new item input
    this.setState({
      list,
      newItem: ""
    });
  }

Since the slice returns new string, the slice might have used to copy the string in the above code.由于slice返回新字符串, slice可能已用于复制上述代码中的字符串。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice

First of all why are you use slice method?首先你为什么使用切片方法? If you used it to trim your value.如果你用它来削减你的价值。 you use The trim() method removes whitespace from both sides of a string.您使用 trim() 方法从字符串的两侧删除空格。 The trim() method does not change the original string. trim() 方法不会更改原始字符串。 Syntax.句法。 string.trim().字符串.trim()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM