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

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

react如何向數(shù)組中追加值_React

作者:otatoz ? 更新時(shí)間: 2022-10-28 編程語(yǔ)言

react向數(shù)組中追加值

首先,渲染一個(gè)隨機(jī)數(shù),每個(gè)一秒變換一次,效果如下:

代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
  <title>數(shù)組追加元素</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一般是通過(guò)setState()方法來(lái)的,只有當(dāng)state或props改變,render()方法才能再次調(diào)用,即組件更新

將生成的隨機(jī)數(shù)放入一個(gè)數(shù)組,效果如下:

代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
  <title>數(shù)組追加元素</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解構(gòu)出來(lái),在將隨機(jī)數(shù)加進(jìn)去

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

react處理數(shù)組的值

1. 追加數(shù)組的值

通過(guò)…運(yùn)算符把數(shù)組之前的值拆分,再在后面追加值

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

2. 刪除下標(biāo)為index的值

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

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

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

欄目分類(lèi)
最近更新