NativeWind 与 React Navigation 一起使用时无法工作

问题描述 投票:0回答:8

NativeWind 不起作用。 当文件 tailwind.config.js 的内容为“./App,{js,jsx,ts,tsx}”时它可以工作,但自从我实现了 React Navigation 后就不再工作了。

tailwind.config.js:

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: ["./App.{js,jsx,ts,tsx}", "./screens/**/*.{js,jsx,ts,tsx}"],
  theme: {
    extend: {},
  },
  plugins: [],
}

package.json:

{
  "name": "inovarlagos",
  "version": "1.0.0",
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web"
  },
  "dependencies": {
    "@react-navigation/native": "^6.0.12",
    "@react-navigation/native-stack": "^6.8.0",
    "expo": "~46.0.9",
    "expo-status-bar": "~1.4.0",
    "nativewind": "^2.0.7",
    "react": "18.0.0",
    "react-native": "0.69.5",
    "react-native-safe-area-context": "4.3.1",
    "react-native-screens": "~3.15.0"
  },
  "devDependencies": {
    "@babel/core": "^7.12.9",
    "tailwindcss": "^3.1.8"
  },
  "private": true
}

App.js:

import * as React from 'react';
import { View, Text } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import HomeScreen from './screens/HomeScreen';

const Stack = createNativeStackNavigator();

function App() {
  return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen name="Home" component={HomeScreen} />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

export default App;

./screens/HomeScreen.js:

import { View, Text } from 'react-native';
import React from 'react';

const HomeScreen = () => {
  return (
    <View className="flex-1 items-center justify-center bg-black">
      <Text className="text-red-200">Futuristik Lagos- Home</Text>      
    </View>
  );
};

export default HomeScreen;

项目结构:

结果(TailWind 不起作用):

javascript reactjs react-native navigation tailwind-css
8个回答
28
投票

我假设您正在使用 NativeWind,因为 React Native 最近终止了对 TailwindCSS 的支持。

首先停止expo服务器。然后,不要使用

expo start
启动它,而是运行
expo start -c
来擦除 NativeWind 添加的缓存并重新启动服务器。

来源:https://www.nativewind.dev/guides/troubleshooting


6
投票

如果您正在使用 Expo,只需将以下代码简单地放入您的 app.js 中

import { NativeWindStyleSheet } from "nativewind";

NativeWindStyleSheet.setOutput({
  default: "native",
});

3
投票

要解决此问题,只需通过

expo start -c
react-native start --reset-cache
清除项目的缓存即可。


1
投票

就我而言,我添加了一个

app
文件夹来存放我的路线。我必须更新我的
content
条目,因为我遵循的入门指南将
tailwind.config.js
作为占位符。
<custom-folder>



1
投票

来自

/** @type {import('tailwindcss').Config} */ module.exports = { content: ['./App.{js,jsx,ts,tsx}', './<custom-folder>/**/*.{js,jsx,ts,tsx}'], // Change ./<custom-folder> to ./app (in my case) theme: { extend: {} }, plugins: [] }

致:

/** @type {import('tailwindcss').Config} */ module.exports = { content: [ "./App.{js,jsx,ts,tsx}", "./<custom directory>/**/*.{js,jsx,ts,tsx}", "./<custom directory>/**/**/*.{js,jsx,ts,tsx}" ], theme: { extend: {}, }, plugins: [], };

解决了我的问题。


0
投票

/** @type {import('tailwindcss').Config} */ module.exports = { content: ["./App.{js,jsx,ts,tsx}", "./src/**/*.{js,jsx,ts,tsx}", "./src/**/**/*.{js,jsx,ts,tsx}"], theme: { extend: {}, }, plugins: [], };



0
投票

0
投票

使用
    plugins: ["nativewind/babel"],
  1. 安装 Expo CLI。
    按照说明运行 
  2. npm install expo-cli --global
  3. expo start -c
    文档
    中。 如果这不起作用,请确保
  4. 内容
  5. 中的路线 Tailwind 配置文件的部分设置正确。
  6. 例如,以下配置文件将仅将 Tailwind 样式应用于
App.js

src/screens 目录和 src/components 目录中的文件: react-native start --reset-cache

© www.soinside.com 2019 - 2024. All rights reserved.