当前位置:网站首页>Le chemin de l'apprentissage immutable - - Adieu à la copie traditionnelle
Le chemin de l'apprentissage immutable - - Adieu à la copie traditionnelle
2022-06-25 04:35:00 【Toc - toc!】
Immutable【Adieu à la copie traditionnelle】
Premier départ:CSDNToc - toc!,Apprendre sans fin
Nuageux30°
The greatest test of courage on earth is to bear defaet without losing heart.
2022/6/24
immutable
Il s'agit d'une bibliothèque tierce qui remplace les méthodes traditionnelles de copie
Avantages:
- L'opération sur un nouvel objet n'affecte pas les données de l'objet original
- Bonne performance
Installation et utilisation
1.Télécharger
npm i immutable
2.Importer
import {Map} from 'immutable'
Map
Syntaxe:
Map(Objet)
Affectation:
set("Nom de la propriété","Nouvelles valeurs")
Valeur:
get("Nom de la propriété")
toJS()
Retourner à l'objet normal
import React, {
Component } from 'react';
/** * 1.Télécharger npm i immutable * 2.Importer import {Map} from 'immutable' */
import {
Map} from 'immutable'
var obj={
name:"Toc - toc!",
age:20
}
var objImmu=Map(obj);
var newobjImmu=objImmu.set("name","pengkeStudy");
// console.log(objImmu,newobjImmu)
//get('Propriétés')Obtenir la valeur
console.log(objImmu.get("name"),newobjImmu.get("name"));
//toJSRetourner à l'objet normal
console.log(objImmu.toJS(),newobjImmu.toJS());
/* //Écrire un class Immu02 extends Component { state={ info:Map({ name:"Toc - toc!", age:20 }) } render() { return ( <div> Immu02 <button onClick={()=>{ this.setState({ info:this.state.info.set('name',"pengkeStudy").set('age',1000) }) }}>Modifier leurs valeurs</button> {this.state.info.get("name")}---- {this.state.info.get("age")} </div> ); } }*/
//écriture II
class Immu02 extends Component {
state={
info:{
name:"Toc - toc!",
age:20
}
}
render() {
return (
<div>
Immu02
<button onClick={
()=>{
let old=Map(this.state.info)
let newImmu=old.set("name","pengkeStudy").set("age",10000)
this.setState({
info:newImmu.toJS()
})
}}>Modifier leurs valeurs</button>
{
this.state.info.name}----
{
this.state.info.age}
</div>
);
}
}
export default Immu02;
NidificationMap
Map L'objet dans l'objet doit également être enveloppé
Map
Peut passermapDe
get
Méthode pour obtenir la valeur à transmettre au composant ., Cela résout parfaitement la mise à jour invalide des composants
shouldComponentUpdate
Pour déterminer si un rafraîchissement est nécessaire
import React, {
Component } from 'react';
import {
Map} from 'immutable'
class Immu03 extends Component {
state={
info:Map({
name:"Toc - toc!",
age:20,
hobbit:Map({
likeone:"Sports",
liketwo:"Apprendre"
})
})
}
render() {
return (
<div>
Immu03
<button onClick={
()=>{
this.setState({
info:this.state.info.set("name"," Apprendre ah ah ah ")
})}}>Modifier</button>
{
this.state.info.get("name")}
<Child hobbit={
this.state.info.get("hobbit")} ></Child>
</div>
);
}
}
class Child extends Component{
shouldComponentUpdate(nextProps,nextState){
//Jugementhobbit Si la valeur de est mise à jour
if(this.props.hobbit===nextProps.hobbit){
return false;
}
return true;
}
render(){
return(
<div>Child</div>
);
}
componentDidUpdate(){
console.log(" Sous - composants mis à jour ");
}
}
export default Immu03;
List
Vous pouvez utiliser la méthode des propriétés du tableau
import React, {
Component } from 'react';
import {
List} from 'immutable'
var arr=List([1,231,1])
let arr2=arr.push(4)// N'affecte pas la structure originale de l'objet
let arr3=arr2.concat([12,323,123])
console.log(arr,arr2,arr3);
class Immu04 extends Component {
state={
favor:List(['Manger','Dormez.','Apprendre','Sports'])
}
render() {
return (
<div>
Immu04
{
this.state.favor.map(item=>{
return <li key={
item}>{
item}</li>
})
}
</div>
);
}
}
export default Immu04;
Mise en œuvre des cas individuels de modification
import {
List,Map } from 'immutable';
import React, {
Component } from 'react';
class Immu05 extends Component {
state={
info:Map({
name:"Toc - toc!",
location:Map({
province:"Jiangxi",
city:"Gian."
}),
hobbit:List(['Dormez.','Apprendre','Frappe au clavier'])
})
}
render() {
return (
<div>
<h1>Modifier les renseignements personnels</h1>
<div>
<button onClick={
()=>{
this.setState({
info:this.state.info.set("name","L'amour de l'apprentissage").set("location",this.state.info.get("location").set("city","Nanchang"))
})
}}>Modifier</button>
{
this.state.info.get("name")}
<br />
{
this.state.info.get("location").get("province")}-
{
this.state.info.get("location").get("city")}
{
this.state.info.get("hobbit").map((item,index)=><li key={
item}>{
item}<button onClick={
()=>{
// console.log(index);
this.setState({
info:this.state.info.set("hobbit",this.state.info.get("hobbit").splice(index,1))
})
}}>Supprimer</button></li>)
}
</div>
</div>
);
}
}
export default Immu05;
AdoptionfromJS、setIn、updateInApporter des améliorations
fromJS
Convertir des objets normaux en ImmutablesetIn()
Affectation de profondeur , Paramètre 1 pour remplir le tableau à modifier , Paramètre II valeur modifiéeupdateIn()
Changement de profondeur , Paramètre 1 pour remplir le tableau à modifier , Argument 2 fonction de rappel ( Le paramètre est l'objet original )
import {
fromJS } from 'immutable';
import React, {
Component } from 'react';
class Immu06 extends Component {
state={
info:fromJS({
name:"Toc - toc!",
location:{
province:"Jiangxi",
city:"Gian."
},
hobbit:['Dormez.','Apprendre','Frappe au clavier']
})
}
render() {
return (
<div>
<h1>Modifier les renseignements personnels</h1>
<div>
<button onClick={
()=>{
this.setState({
info:this.state.info.setIn(["name"],"L'amour de l'apprentissage").setIn(["location","city"],"Nanchang")//setIn(" Le paramètre 1 est un tableau ","Valeur modifiée")
})
}}>Modifier</button>
{
this.state.info.get("name")}
<br />
{
this.state.info.get("location").get("province")}-
{
this.state.info.get("location").get("city")}
{
this.state.info.get("hobbit").map((item,index)=><li key={
item}>{
item}<button onClick={
()=>{
// console.log(index);
// this.setState({
// info:this.state.info.setIn(["hobbit",index],"")
// })
//updateIn( Objet à modifier ,Fonction de rappel( Le paramètre est l'objet original ))
this.setState({
info:this.state.info.updateIn(["hobbit"],(list)=>list.splice(index,1))
})
}}>Supprimer</button></li>)
}
</div>
</div>
);
}
}
export default Immu06;
C'est comme ça que les frais de scolarité Immutable~
边栏推荐
- Cascading deletion of gbase 8s
- 坐标系左乘右乘
- Classification of gbase 8s locks
- 记录小知识点
- Sourcetree pulls the code and prompts to fill in authentic, but the configuration cannot change the user
- GBASE 8s的多线程结构
- SQL injection details
- Intel 13th generation core showed its true colors for the first time: 68mb cache improved significantly
- Laravel document sorting 8. Middleware
- Coinlist queuing tutorial to improve the winning rate
猜你喜欢
机器学习深度学习——向量化
OBS Browser+浏览器的基本使用
CTF_ Web: deserialization of learning notes (II) CTF classic test questions from shallow to deep
A detailed summary of four handshakes (or four waves) over TCP connections
简单的恶意样本行文分析-入门篇
关于TCP连接四次握手(或者叫四次挥手)的详细总结
LabVIEW开发气体调节器
Value transfer between parent and child components of wechat applet
cnpm : 无法加载文件 C:\Users\Administrator\AppData\Roaming\npm\cnpm.ps1,因为在此系统上禁止运行脚本。
【无标题】
随机推荐
IntStream API介绍
js中的concat()
PostgreSQL数据库WAL——RM_HEAP_ID日志记录动作
CTF_ Web: Advanced questions of attack and defense world expert zone WP (19-21)
unity Quad剔除背面并剔除透明部分的shader
Can Navicat directly operate the Android database SQLite
深度学习——几种学习类型
我的IC之旅——资深芯片设计验证工程师成长——“胡”说IC工程师完美进阶
LabVIEW development gas regulator
论文笔记: 多标签学习 ESMC (没看懂, 还没写出来, 暂时放这里占个位置)
LabVIEW开发气体调节器
js的call()和apply()
GBASE 8s的数据导入和导出
UCLA | generative pre training for black box optimization
Retrofit source code analysis
GBase 8s的封锁技术的基本介绍
Musk released humanoid robot. Why is AI significant to musk?
2020.3.3 notes async/await and promise and Then processes and threads
GBASE 8s的包
Communication problems in parent and child components of uniapp