網(wǎng)站首頁(yè) 編程語(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
相關(guān)推薦
- 2022-02-24 React插槽使用方法_React
- 2022-10-15 python?實(shí)現(xiàn)syslog?服務(wù)器的詳細(xì)過(guò)程_python
- 2022-06-19 C++詳細(xì)分析講解函數(shù)參數(shù)的擴(kuò)展_C 語(yǔ)言
- 2023-03-27 Android?WorkManager實(shí)現(xiàn)后臺(tái)定時(shí)任務(wù)流程詳解_Android
- 2022-08-03 MongoDB數(shù)據(jù)庫(kù)條件查詢(xún)技巧總結(jié)_MongoDB
- 2022-04-01 OpenCV實(shí)現(xiàn)簡(jiǎn)單錄屏功能_C 語(yǔ)言
- 2024-07-18 MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- 2023-01-05 C++模板?index_sequence使用示例詳解_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支