I was trying to set up tailwind-rn in my project, but it seems impossible. The documentation says you should use yarn add tailwind-rn to install, then npx setup-tailwind-rn to set it up. This is a code example on the github page:
import React from 'react';
import {SafeAreaView, View, Text} from 'react-native';
import {useTailwind} from 'tailwind-rn';
const Hello = () => {
const tailwind = useTailwind();
return (
<SafeAreaView style={tailwind('h-full')}>
<View style={tailwind('pt-12 items-center')}>
<View style={tailwind('bg-blue-200 px-3 py-1 rounded-full')}>
<Text style={tailwind('text-blue-800 font-semibold')}>
Hello Tailwind
</Text>
</View>
</View>
</SafeAreaView>
);
};
export default Hello;
My code looks like this:
import { StyleSheet, Text, View } from 'react-native';
import {useTailwind} from 'tailwind-rn';
export default function App() {
const tailwind = useTailwind();
return (
<View style={tailwind('text-white')}>
<Text>Open up App.js to start working on your app!</Text>
</View>
);
}
The code does not throw any error, but simply produces nothing. I also checked the value of tailwind('text-white'), which seems to be just an empty object. Why does that not work?
Add this to your dependencies (package.json)
"tailwind-react-native-classnames": "^1.5.1",
and Import it in your project page like this,
import tw from "tailwind-react-native-classnames";
Finally use the tailwind styles like this,
style={tw`flex-1`}
<View style={[tw`bg-gray-200`, { height: 0.6 }]} />
And finally your code will work.
Related
To recreate
Create React TS app with Vite
npm init vite#latest vite-project --template react
Create sample pdf document
import React from 'react';
import pdf from '#react-pdf/renderer';
const { Page, Text, View, Document, StyleSheet } = pdf;
// Create styles
const styles = StyleSheet.create({
page: {
flexDirection: 'row',
backgroundColor: '#E4E4E4',
},
section: {
margin: 10,
padding: 10,
flexGrow: 1,
},
});
// Create Document Component
export const MyDocument = () => (
<Document>
<Page size='A4' style={styles.page}>
<View style={styles.section}>
<Text>Section #1</Text>
</View>
<View style={styles.section}>
<Text>Section #2</Text>
</View>
</Page>
</Document>
);
Delete the contents of App.ts. And add the following code from #react-pdf/renderer's example
import React from 'react';
import ReactDOM from 'react-dom';
import { PDFViewer } from '#react-pdf/renderer';
const App = () => (
<PDFViewer>
<MyDocument />
</PDFViewer>
);
Run the development server and check console for errors.
I tried to do it with create react app and everything worked fine. With Vite is that the error happens.
I solved it!
You just need to manually install the following package.
npm i events
React-native styling using Nativewind works fine if the styling is done at App.Js. Like here:
<NavigationContainer>
<View>
<Text className = "text-red-500">Hello</Text>
</View>
</NavigationContainer>
But if the styling is done at component level, then it doesn't work anymore.
return (
<NavigationContainer>
<HomeScreen/>
</NavigationContainer>
);
HomeScreen Component:
import { View, Text } from 'react-native'
import React from 'react'
const HomeScreen = () => {
return (
<View>
<Text className = "text-red-500">Hello</Text>
</View>
)
}
export default HomeScreen
You'll need to tell nativewind the locations of where your components reside by adding this to your tailwind.config.ts
content: ["./App.{js,jsx,ts,tsx}", "./<custom directory such as src>/**/*.{js,jsx,ts,tsx}"],
reference: https://www.nativewind.dev/quick-starts/react-native-cli
If you setup folder in tailwind.config.js and still not work then delete nodemodule then install it again and it will work.
This works for me:
content: ["./App.{js,jsx,ts,tsx}", "./<custom directory such as src>/**/*.{js,jsx,ts,tsx}"]
attached is the code below for a little app I am making with React Native. I was trying to use react icons but for some reason they are not working and idk why. I imported them and everything and they should be working but I keep getting this error. Does anyone know why my react icons are not functioning properly? Thanks in advance.
import { View, Text, SafeAreaView, Image } from 'react-native';
import React, { useLayoutEffect } from 'react';
import { useNavigation } from '#react-navigation/native';
import { FaBeer } from 'react-icons/fa';
const HomeScreen = () => {
const navigation = useNavigation();
useLayoutEffect(() => {
navigation.setOptions({
headerShown: false,
});
}, []);
return (
<SafeAreaView>
<Text className="text-red-500">
<View className="flex-row pb-3 items-center mx-4 space-x-2">
<Image
source={{
url:"https://links.papareact.com/wru",
}}
className="h-7 w-7 bg-gray-300 p-4 rounded-full"
/>
<View>
<Text className="font-bold text-gray-400 text-xs">
Deliver Now
</Text>
<Text className="font-bold text-xl">Current Location
<FaBeer />
</Text>
</View>
</View>
</Text>
</SafeAreaView>
);
};
export default HomeScreen;
react-icons doesn't work with react native (i think). You need to use react-native-vector-icons (https://www.npmjs.com/package/react-native-vector-icons)
you cant use normal react icons since in react native icons can be either an image or can be SVG, so for SVG icons, you need to add react-native-svg package.
You should install as #dev404 said https://github.com/oblador/react-native-vector-icons
Also do follow their installation process for both android and ios.
YOu can check the below available icon sets.
Hope it helps, feel free for doubts
I am following this YouTube tutorial https://www.youtube.com/watch?v=qJaFIGjyRms and at the 20 minute mark he imports Tailwind as tw then proceeds to use it in app.js I have done the exact same thing yet I am getting the error
"TypeError: (0, _tailwindRn.tw) is not a function. (In '(0, _tailwindRn.tw)("justify-center items-center")', '(0, _tailwindRn.tw)' is undefined)"
screenshot
my code:
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, View } from 'react-native';
import {tw} from 'tailwind-rn';
export default function App() {
return (
<View style={tw("justify-center items-center")}>
<Text>Open up App.js to start working on your app!</Text>
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
Hi I was facing the same problem
I used npx setup-tailwind-rn to set up tailwind. The below code worked for me:
import { StatusBar } from 'expo-status-bar';
import { Button, StyleSheet, Text, View } from 'react-native';
import React from 'react';
//import tw from 'tailwind-rn'
import {useTailwind} from 'tailwind-rn'
export default function App() {
const tailwind = useTailwind();
return (
<View style={tailwind('flex-1 justify-center items-center')}>
<Text>Hello World!!</Text>
<Button title='Click-me'></Button>
</View>
);
}
You need to install the tailwind-rn npm package.
More info https://www.npmjs.com/package/tailwind-rn
If you go back and look at the tutorial, they are using
import tw from 'tailwind-rn';
not
import {tw} from 'tailwind-rn';
You can name the default export of a library whatever you like in the import statement.
import TailWind from 'tailwind-rn';
import t from 'tailwind-rn';
etc.
StatusBar backgroundColor prop doesn't work, in Expo Snack too. Work only props hidden and barStyle. Code example from Expo Snack:
import * as React from 'react';
import { Text, View, StyleSheet, StatusBar } from 'react-native';
import Constants from 'expo-constants';
// You can import from local files
import AssetExample from './components/AssetExample';
// or any pure javascript modules available in npm
import { Card } from 'react-native-paper';
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<StatusBar backgroundColor="white" barStyle="dark-content" hidden={false} translucent={false}/>
<Text style={styles.paragraph}>
Change code in the editor and watch it change on your phone! Save to get a shareable url.
</Text>
<Card>
<AssetExample />
</Card>
</View>
);
}
}
I also tried to add a style in the app.json:
{
"expo": {
...
"androidStatusBar": {
"backgroundColor": "#ffffff"
}
}
}
and
"androidStatusBarColor": "#ffffff", "androidStatusBar": { "barStyle": "dark-content", "backgroundColor": "#ffffff" }
What do i wrong?
Thanks!
It's an Expo problem, the community has been asking for a solution for some time. But it seems that in the next versions of Expo the problem will be solved.
You can follow this situation here: https://github.com/expo/expo/issues/2813