日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

react如何向數組中追加值_React

作者:otatoz ? 更新時間: 2022-10-28 編程語言

react向數組中追加值

首先,渲染一個隨機數,每個一秒變換一次,效果如下:

代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
  <title>數組追加元素</title>
  <script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
  <script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
  <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
</head>
<body>
 
<div id="xpf"></div>
<script type="text/babel">
class Xpf extends React.Component {
  constructor(props){
    super(props);
    this.state = {
      random:Math.random()
    }
  }
  
  componentWillMount(){
    setInterval(() => {
      this.setState({
        random:Math.random()
      })
    }, 1000);
  }
 
  render() {
    let {random} = this.state;
      return (
        <div>
          <div>
            {random}
          </div> 
          
        </div>
      );
  }
} 
 
ReactDOM.render(
	<Xpf />,
	document.getElementById('xpf')
);
 
</script>
 
</body>
</html>

注意:組件更新有兩種方式props或state的改變,而改變state一般是通過setState()方法來的,只有當state或props改變,render()方法才能再次調用,即組件更新

將生成的隨機數放入一個數組,效果如下:

代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
  <title>數組追加元素</title>
  <script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
  <script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
  <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
</head>
<body>
 
<div id="xpf"></div>
<script type="text/babel">
class Xpf extends React.Component {
  constructor(props){
    super(props);
    this.state = {
      random:Math.random(),
      arr:[1,2,3]
    }
  }
  
  componentWillMount(){
    setInterval(() => {
      this.setState({
        random:Math.random(),
        arr:[...this.state.arr,Math.random()]
      })
    }, 1000);
  }
 
  render() {
    let {random,arr} = this.state;
      return (
        <div>
          <div>
            {random}
          </div> 
          <ul>
            {
              arr.map((item,index)=>{
                return ( <li key={index}>{item}</li>)
              })
            }
          </ul>  
        </div>
      );
  }
} 
 
ReactDOM.render(
	<Xpf />,
	document.getElementById('xpf')
);
 
</script>
 
</body>
</html>

使用...this.state.arr將arr解構出來,在將隨機數加進去

注意:不能使用 arr : this.state.arr.push(Math.random()),不能使用在原數組的基礎上修改的方法,例如push之類,可以使用concat方法或者ES6數組拓展語法

react處理數組的值

1. 追加數組的值

通過…運算符把數組之前的值拆分,再在后面追加值

this.state = {
? arr: []
}
add(){
? this.setState((prevSatet) => ({
? ? arr: [...prevSatet.arr, [追加的值] ]
? }))
}

2. 刪除下標為index的值

把state中arr的值拷貝到新的arr中,刪除新arr下標為index的值后,再重新賦值回arr

主要不要直接刪除state中arr的值,然后賦值回去,這樣違反immutable的規則

this.state = {
? arr: []
}
delete(index){?
? this.setState((prevState) => {
? ? const arr = [...prevState.list]
? ? arr.splice(index, 1)
? ? return { arr }
? })
}

原文鏈接:https://blog.csdn.net/qq_42720683/article/details/95594398

欄目分類
最近更新