網站首頁 編程語言 正文
有些時候,我們不希望使用某些組件,而是想使用其他組件。這時候,我們可以使用一個名為?no-restricted-imports?的eslint 規則,該規則允許你指定一個或多個組件的名稱,以防止使用這些組件。
no-restricted-imports
?是 eslint 自帶的一個規則,我們不需要額外引入一個插件。
限制使用 Touchable
假如我們希望小伙伴們不要使用?Touchable
?系列組件,而是使用?Pressable
?組件,那么我們可以這樣寫:
// .eslintrc.js module.exports = { rules: { "no-restricted-imports": [ "error", { paths: [ { name: "react-native", importNames: [ "TouchableOpacity", "TouchableHighlight", "TouchableWithoutFeedback", "TouchableNativeFeedback", ], message: "Use Pressable instead", }, ], }, ], }, }
限制使用 Image
又譬如,我們希望小伙伴們使用?FastImage?組件,而不是使用?Image
?組件,那么我們可以這樣寫:
// .eslintrc.js module.exports = { rules: { "no-restricted-imports": [ "error", { paths: [ { name: "react-native", importNames: ["Image"], message: "Use FastImage from react-native-fast-image instead", }, ], }, ], }, }
同時限制兩者
如果我們既要限制使用?Touchable
?組件,又要限制使用?Image
?組件,那么我們可以這樣寫:
// .eslintrc.js module.exports = { rules: { "no-restricted-imports": [ "error", { paths: [ { name: "react-native", importNames: [ "TouchableOpacity", "TouchableHighlight", "TouchableWithoutFeedback", "TouchableNativeFeedback", "Image", ], message: "這個提示怎么寫?", }, ], }, ], }, }
但問題是,?message
?怎么寫?
我們希望,如果小伙伴使用?Touchable
?組件,那么就提示他?Use Pressable instead?,如果小伙伴使用?Image
?組件,那么就提示他?Use FastImage from react-native-fast-image instead?。
經過作者?一番調研?,發現可以使用no-restricted-syntax 來達到更精確的控制導入目的:
// .eslintrc.js module.exports = { rules: { "no-restricted-syntax": [ "error", { selector: "ImportDeclaration[source.value='react-native'] > ImportSpecifier[imported.name=/Touchable\\w*/]", message: "Use Pressable instead", }, { selector: "ImportDeclaration[source.value='react-native'] > ImportSpecifier[imported.name='Image']", message: "Use FastImage from react-native-fast-image instead", }, ], }, }
效果如下圖所示:
當然,對于要限定的某些模塊,如果 no-restricted-imports 能滿足需求,則優先使用它。
示例
這里有?一個示例?,供你參考。
原文鏈接:https://juejin.cn/post/7127057745081008165
相關推薦
- 2023-01-09 C#使用stackalloc分配堆棧內存和非托管類型詳解_C#教程
- 2022-02-28 [rejected] master -> master (fetch first) error: f
- 2022-07-16 MyBatis查詢時數據表字段名與實體類屬性名不一致
- 2024-03-22 解決springboot錯誤頁面exception、message取值為空
- 2022-10-06 一文掌握python中的時間包_python
- 2022-11-30 Git基礎學習之分支基本操作詳解_相關技巧
- 2022-11-27 Rust語言中的String和HashMap使用示例詳解_Rust語言
- 2022-11-14 .NET?Core?Web?APi類庫內嵌運行的方法_實用技巧
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支