如何配置React Native项目以使用CSS Modules?
- 内容介绍
- 文章标签
- 相关推荐
本文共计832个文字,预计阅读时间需要4分钟。
目录 + 安装依赖和配置 + 使用 + 示例 + 前端工程师希望也能参与开发web应用,使用CSS Modules开发React Native。本文将介绍如何在React Native中使用CSS Modules。
安装依赖和配置首先,确保你的开发环境已安装Node.js和React Native CLI。然后,创建一个新的React Native项目:
bashnpx react-native init MyProjectcd MyProject
安装CSS Modules:
bashnpm install --save styled-components
配置项目以使用CSS Modules:
在`package.json`中添加以下配置:
jsoncss: { preprocessor: postcss}
在`index.js`中引入CSS Modules:
jsximport './App.module.css';
使用CSS Modules现在,你可以在组件中直接使用CSS Modules:
jsximport React from 'react';import { StyleSheet, View, Text } from 'react-native';import { AppRegistry } from 'react-native';
const App=()=> { return ( Hello, CSS Modules! );};
const styles=StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF', }, title: { fontSize: 20, textAlign: 'center', margin: 20, },});
AppRegistry.registerComponent('MyProject', ()=> App);
示例以下是一个使用CSS Modules的示例:
css.title { color: red; font-size: 24px;}
在组件中使用:
jsximport React from 'react';import { StyleSheet, View, Text } from 'react-native';import { AppRegistry } from 'react-native';
const App=()=> { return ( Hello, CSS Modules! );};
const styles=StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF', }, title: { ...styles.title, },});
AppRegistry.registerComponent('MyProject', ()=> App);
总结通过以上步骤,你可以在React Native项目中使用CSS Modules来开发样式。这种方式使得样式更加模块化,易于管理和维护。
目录
- 安装依赖和配置
- 使用
- 示例
有些前端工程师希望也能像开发 web 应用那样,使用 CSS Modules 来开发 React Native。本文将介绍如何在 React Native 中使用 CSS Modules。
安装依赖和配置
首先安装react-native-sass-transformer使得我们可以在 React Native 应用中使用 sass 样式。
yarn add react-native-sass-transformer sass -D
参考如下配置,修改 metro.config.js 文件
const { getDefaultConfig } = require("metro-config") module.exports = (async () => { const { resolver: { sourceExts }, } = await getDefaultConfig() return { transformer: { babelTransformerPath: require.resolve("react-native-sass-transformer"), }, resolver: { sourceExts: [...sourceExts, "scss", "sass"], }, } })()
我们还需要安装另外两个依赖
yarn add babel-plugin-react-native-classname-to-style \ babel-plugin-react-native-platform-specific-extensions -D
修改 babel.config.js 文件,配置刚刚安装的两个插件
module.exports = { presets: ["module:metro-react-native-babel-preset"], plugins: [ "react-native-classname-to-style", [ "react-native-platform-specific-extensions", { extensions: ["scss", "sass"], }, ], ], }
因为我们的项目使用了 Typescript,为了避免类型警告,在项目中添加一个 global.d.ts 文件,内容如下
declare module "*.scss"
使用
React Native CSS Modules 支持@mixin @include @extend等操作
@mixin lightFontStyle($fontColor: rgb(0, 0, 0)) { font-size: 22px; font-family: $sencodary-font-light; letter-spacing: 0.96px; color: $fontColor; } .input { @include lightFontStyle($fontColor: rgb(39, 39, 39)); padding: 12px 20px 40px; flex: 1; } .disabled { @extend .input; color: rgb(99, 99, 99); }
CSS Modules 也可以很好的和 StyleSheet 一起工作
// App.scss @import "./theme/font.scss"; .welcome { font-family: $primary-font; font-size: 17px; text-align: center; }
//App.tsx import React from "react" import { Text, StyleSheet } from "react-native" import scss from "./App.scss" function Welcome(props: Props) { return <Text style={[scss.welcome, styles.text]}>Hello {props.name}!</Text> } const styles = StyleSheet.create({ text: { backgroundColor: "transparent", margin: 8, }, })
示例
这里有一个示例,供你参考。
到此这篇关于如何在ReactNative中使用CSSModules的文章就介绍到这了,更多相关ReactNative使用CSSModules内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!
本文共计832个文字,预计阅读时间需要4分钟。
目录 + 安装依赖和配置 + 使用 + 示例 + 前端工程师希望也能参与开发web应用,使用CSS Modules开发React Native。本文将介绍如何在React Native中使用CSS Modules。
安装依赖和配置首先,确保你的开发环境已安装Node.js和React Native CLI。然后,创建一个新的React Native项目:
bashnpx react-native init MyProjectcd MyProject
安装CSS Modules:
bashnpm install --save styled-components
配置项目以使用CSS Modules:
在`package.json`中添加以下配置:
jsoncss: { preprocessor: postcss}
在`index.js`中引入CSS Modules:
jsximport './App.module.css';
使用CSS Modules现在,你可以在组件中直接使用CSS Modules:
jsximport React from 'react';import { StyleSheet, View, Text } from 'react-native';import { AppRegistry } from 'react-native';
const App=()=> { return ( Hello, CSS Modules! );};
const styles=StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF', }, title: { fontSize: 20, textAlign: 'center', margin: 20, },});
AppRegistry.registerComponent('MyProject', ()=> App);
示例以下是一个使用CSS Modules的示例:
css.title { color: red; font-size: 24px;}
在组件中使用:
jsximport React from 'react';import { StyleSheet, View, Text } from 'react-native';import { AppRegistry } from 'react-native';
const App=()=> { return ( Hello, CSS Modules! );};
const styles=StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF', }, title: { ...styles.title, },});
AppRegistry.registerComponent('MyProject', ()=> App);
总结通过以上步骤,你可以在React Native项目中使用CSS Modules来开发样式。这种方式使得样式更加模块化,易于管理和维护。
目录
- 安装依赖和配置
- 使用
- 示例
有些前端工程师希望也能像开发 web 应用那样,使用 CSS Modules 来开发 React Native。本文将介绍如何在 React Native 中使用 CSS Modules。
安装依赖和配置
首先安装react-native-sass-transformer使得我们可以在 React Native 应用中使用 sass 样式。
yarn add react-native-sass-transformer sass -D
参考如下配置,修改 metro.config.js 文件
const { getDefaultConfig } = require("metro-config") module.exports = (async () => { const { resolver: { sourceExts }, } = await getDefaultConfig() return { transformer: { babelTransformerPath: require.resolve("react-native-sass-transformer"), }, resolver: { sourceExts: [...sourceExts, "scss", "sass"], }, } })()
我们还需要安装另外两个依赖
yarn add babel-plugin-react-native-classname-to-style \ babel-plugin-react-native-platform-specific-extensions -D
修改 babel.config.js 文件,配置刚刚安装的两个插件
module.exports = { presets: ["module:metro-react-native-babel-preset"], plugins: [ "react-native-classname-to-style", [ "react-native-platform-specific-extensions", { extensions: ["scss", "sass"], }, ], ], }
因为我们的项目使用了 Typescript,为了避免类型警告,在项目中添加一个 global.d.ts 文件,内容如下
declare module "*.scss"
使用
React Native CSS Modules 支持@mixin @include @extend等操作
@mixin lightFontStyle($fontColor: rgb(0, 0, 0)) { font-size: 22px; font-family: $sencodary-font-light; letter-spacing: 0.96px; color: $fontColor; } .input { @include lightFontStyle($fontColor: rgb(39, 39, 39)); padding: 12px 20px 40px; flex: 1; } .disabled { @extend .input; color: rgb(99, 99, 99); }
CSS Modules 也可以很好的和 StyleSheet 一起工作
// App.scss @import "./theme/font.scss"; .welcome { font-family: $primary-font; font-size: 17px; text-align: center; }
//App.tsx import React from "react" import { Text, StyleSheet } from "react-native" import scss from "./App.scss" function Welcome(props: Props) { return <Text style={[scss.welcome, styles.text]}>Hello {props.name}!</Text> } const styles = StyleSheet.create({ text: { backgroundColor: "transparent", margin: 8, }, })
示例
这里有一个示例,供你参考。
到此这篇关于如何在ReactNative中使用CSSModules的文章就介绍到这了,更多相关ReactNative使用CSSModules内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

