当前位置:网站首页>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 immutable2.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é
MapPeut passermapDe
getMéthode pour obtenir la valeur à transmettre au composant ., Cela résout parfaitement la mise à jour invalide des composants
shouldComponentUpdatePour 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
fromJSConvertir 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~
边栏推荐
- Read lsd-slam: large scale direct monolithic slam
- GBASE 8s 索引B+树
- 冰冰学习笔记:循环队列的实现
- CMD operation MySQL in Windows
- Doubts about judging the tinyint field type of MySQL
- Failed to install redis interface
- Unity Quad culls shaders with back faces and transparent parts
- CTF_ Web: deserialization learning notes (I) classes and objects in PHP
- Data import and export for gbase 8s
- What is data persistence?
猜你喜欢

Coinlist queuing tutorial to improve the winning rate

单元测试覆盖率

UCLA | 用于黑盒优化的生成式预训练

Finereport (sail soft) handling the problem that the histogram data label is blocked

CTF_ Web: Advanced questions of attack and defense world expert zone WP (1-4)

冰冰学习笔记:循环队列的实现

Coinlist how to operate the middle lot number security tutorial

CTF_ Web: Learn flask template injection (SSTI) from 0

Gbase 8s overall architecture

Nodejs connects to MySQL through heidisql, and ER appears_ BAD_ DB_ ERROR: Unknown database 'my_ db_ books'
随机推荐
Cnpm: unable to load file c:\users\administrator\appdata\roaming\npm\cnpm PS1 because running scripts is prohibited on this system.
Lecture record: data processing methods and applications of various spatial geodetic techniques
Leetcode points to the leetcode road of offering II 091 house painting [dynamic planning] heroding
UCLA | generative pre training for black box optimization
GBASE 8s的触发器
Failed to install redis interface
Finereport (sail soft) handling the problem that the histogram data label is blocked
5 key indicators of SEO: ranking + traffic + session + length of stay + bounce rate
Laravel document sorting 7. View
Gbase 8s stored procedure flow control
关于TCP连接四次握手(或者叫四次挥手)的详细总结
What is the storage engine and the three common database storage engines for MySQL
Introduction to the isolation level of gbase 8s
CTF_ Web: how to recognize and evaluate a regular expression
Laravel document sorting 3. CSRF protection
English Grammar - pronunciation rules
无法安装redis接口
OBS Browser+浏览器的基本使用
A detailed summary of TCP connection triple handshake
Easyrecovery15 very easy to use computer data recovery software