網(wǎng)站首頁 編程語言 正文
最近遇到了一個需求
先簡單介紹下項(xiàng)目:該項(xiàng)目有一個父類組件,該組件用于提供頁面所需的公共數(shù)據(jù)及內(nèi)容,后續(xù)所有的組件都需要繼承該組件獲得公共的數(shù)據(jù)。
現(xiàn)在遇到的問題是:我需要在某個組件中引入一個js配置文件,并且該配置文件中的數(shù)據(jù)又必須來子組件,因?yàn)樵摂?shù)據(jù)從父類組件中繼承下來。
那么思路是這樣的:首先我們將配置文件寫成導(dǎo)出一個方法,并且該方法返回配置數(shù)據(jù)的形式,然后在組件文件中引入該配置文件,然后在state中定義一個狀態(tài)存儲配置文件返回的數(shù)據(jù)(也就是調(diào)用配置文件導(dǎo)出的方法,并且將配置文件需要的值傳遞進(jìn)去)
組件代碼
import React, { Component } from "react";
import { babel } from "./configOption";//引入配置文件
// 這部分原本應(yīng)該是在繼承組件componentDidMount生命周期中通過請求接口獲取相應(yīng)數(shù)據(jù)
// 這里為了方便,我直接使用變量定義一些數(shù)據(jù)
const data = {
female: "女",
male: "男",
};
export default class App extends Component {
state = {
sex: "",
source: babel(data),//將配置問價需要的參數(shù)傳遞進(jìn)去,并且將配置好的對象返回,在頁面中使用
};
componentDidMount() {}
render() {
const options = this.state.source.sexList.map((it,i) => <option value={it.value} key={i}>{it.label}</option>)
return (
<div>
<select name="" id="">
{options}
</select>
</div>
);
}
}
配置文件configOption.js代碼
//將需要的數(shù)據(jù)對象傳入
export const babel = (location) => ({
sexList: [
{ label: location.male, value: "male" },
{ label: location.female, value: "female" },
],
});
原文鏈接:https://blog.csdn.net/huanan__/article/details/127250136
相關(guān)推薦
- 2022-06-06 一文搞懂Redis中String數(shù)據(jù)類型_Redis
- 2022-11-27 Python線性網(wǎng)絡(luò)實(shí)現(xiàn)分類糖尿病病例_python
- 2022-05-03 EF使用Code?First模式給實(shí)體類添加復(fù)合主鍵_實(shí)用技巧
- 2022-11-22 ubuntu20.04中vscode使用ROS的詳細(xì)方法_C 語言
- 2022-09-30 Nginx使用ngx_http_upstream_module實(shí)現(xiàn)負(fù)載均衡功能示例_nginx
- 2023-03-11 Golang的Fork/Join實(shí)現(xiàn)代碼_Golang
- 2023-04-26 Python實(shí)現(xiàn)計(jì)算函數(shù)或程序執(zhí)行時間_python
- 2023-05-15 GoLang中的加密方法小結(jié)_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- 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)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤: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)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支