網(wǎng)站首頁 編程語言 正文
react定義變量并使用
這里面涉及到了
- 1、變量如何定義
- 2、變量如何進(jìn)行改變
- 3、方法如何調(diào)用
都寫有詳細(xì)的注釋大家可詳細(xì)觀看適合剛學(xué)習(xí)react的新同學(xué)
class Packaging extends React.Component{ // react 類組件
constructor(props) {
super(props);
this.state = {
// 進(jìn)行變量定義(會vue的同學(xué):這個地方就相當(dāng)于 data 的return里所定義的)
// 例如
name:'定義name'
};
this.getNameData();// 調(diào)用方法
}
// 定義方法-寫這個方法是為了給大家操作一下怎么改變定義的數(shù)據(jù)
getNameData() {
http.get('接口名稱').then(res => {
// 第一種
this.setState({ // 使用this.setState來進(jìn)行改變變量
name: res.data.name
});
console.log(this.state.dataObj) // 打印不到的
// 第二種
this.setState({
name: res.data.name
},() => {
console.log(this.state.dataObj) // 可以打印到
})
}).catch(error => {
console.error(error)
})
}
render() {
return <div className="className">// className定義div的class名稱
{this.state.name}
</div>
}
}
react全局變量的設(shè)置
新建一個 util文件夾 ---> tool.jsx
window._= {
?? ??? ?/**
?? ??? ? * 存儲localStorage
?? ??? ? */
?? ??? ?setStore:(name, content) =>{
?? ??? ??? ?if (!name) return;
?? ??? ??? ?if (typeof content !== 'string') {
?? ??? ??? ??? ?content = JSON.stringify(content);
?? ??? ??? ?}
?? ??? ??? ?window.localStorage.setItem(name, content);
?? ??? ?},
?? ??? ?/**
?? ??? ? * 獲取localStorage
?? ??? ? */
?? ??? ?getStore:(name) => {
?? ??? ??? ?if (!name) return;
?? ??? ??? ?return window.localStorage.getItem(name);
?? ??? ?},
?? ??? ?/**
?? ??? ? * 清除localStorage
?? ??? ? */
?? ??? ?clearStore:() => {
?? ??? ??? ?window.localStorage.clear();
?? ??? ?},
?? ??? ?
?? ??? ?getQueryStringByName: function (name) {
?? ??? ??? ?var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
?? ??? ??? ?var r = window.location.search.substr(1).match(reg);
?? ??? ??? ?var context = "";
?? ??? ??? ?if (r != null)
?? ??? ??? ??? ?context = r[2];
?? ??? ??? ?reg = null;
?? ??? ??? ?r = null;
?? ??? ??? ?return context == null || context == "" || context == "undefined" ? "" : context;
?? ??? ?}
?? ?
}
在入口文件app.jsx里面引入
import ?"util/tool.jsx";
然后在其余的組件里面就可以訪問到這個變量對象:_
總結(jié)
原文鏈接:https://blog.csdn.net/weixin_43609391/article/details/114977960
相關(guān)推薦
- 2022-05-27 對Entity?Framework?Core進(jìn)行單元測試_實用技巧
- 2021-12-09 Typora自動編號的具體操作_其它綜合
- 2022-07-02 Prometheus+Grafana監(jiān)控Docker容器和Linux主機(jī)
- 2022-08-25 Python??中的pass語句語法詳析_python
- 2023-07-10 Gateway服務(wù)網(wǎng)關(guān)
- 2023-03-21 styled-components?性能詳解_React
- 2022-12-12 flutter中如何使用和擴(kuò)展ThemeData實現(xiàn)詳解_Dart
- 2022-07-07 淺談Qt實現(xiàn)HTTP的Get/Post請求_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支