I want to style a component with my own custom className that has certain properties. Tailwind lets you do this through App.css, but in a react native application there is none. How do you add say a '.title' class to tailwind with certain properties?
.title {
font-size: 24px
font-weight: 800
}
Applying it to Text JSX object:
<Text className="title">Title Text!</Text>
I figured out this method of styling multiple of the same components. Nativewind and Tailwind suggest not using this method, but it works for both.
In your tailwind.config.js file, add the following line of code above your module.exports:
const plugin = require("tailwindcss/plugin");
module.exports = {...}
Then in your plugins list, add:
plugin(function ({ addComponents }) {
addComponents({
".title": {
fontWeight: 800,
fontSize: 24,
},
});
}),
Make sure your styling uses the same property names as react-native, and NOT css (fontWeight vs font-weight). Restart your server, and it should work!
You can use twrnc npm package for this. Install it from here.
Import it like this in the file at the top.
import tw from "twrnc";
And use it like this
<Text style={tw` text-2xl font-extrabold`}>Title Text!</Text>
I have a reactnative project, I added:
npm install tailwind-rn
Imported it into the App.js file
**import tw from 'tailwind-rn';
export default function App() {
return (
<View style={tw('bg-blue-200 px-3 py-1 rounded-full')}>
<Text>Hey there!</Text>
</View>
);
}**
When I launch it in Expo am stuck at the Splashscreen, it doesnt go further. But when I remove the tailwind import and publish it passes the Splashscreen and loads normally.
What can be the cause? Why cant tailwimd-rn work with the app? Once I implement it an run on expo it just get stuck at the Splashscreen.
Instead of putting '' in the style tag, use `` and remove the () bracket in the style tag.
I'm trying to use a custom font on my vue native app, but dontt find how to do this. I've tried use #font-face.enter image description here
i found something like Font.loadAsync from react native, but i don't know how to use this
I been using expo documentation, perhaps this can leads you to a solution
Edit:
Well after installing the given font package, I imported expo-font in my app.vue file then I loaded inside the google font package and that's all
//App.vue
import * as Font from "expo-font";
Font.loadAsync({
VT323_Regular: require("./node_modules/#expo-google-fonts/vt323/VT323_400Regular.ttf"),
});
//export default { ...
.myText {
font-family: VT323_Regular;
}
<template>
<view class="container">
<text class="myText">ABCDEF</text>
</view>
</template>
I want to turn this section of code into a reusable component so I don't have to write the same thing out 5 times.
<TouchableOpacity onPress={console.log('pressed')}>
<Image
source={require('../img/button_australia.png')}
/>
</TouchableOpacity>
The new component I made to mirror this is as follows:
import React from 'react';
import { Image, TouchableOpacity } from 'react-native';
const ImgButton = ({ onPress, img }) => {
return (
<TouchableOpacity onPress={onPress}>
<Image
source={require(img)}
/>
</TouchableOpacity>
);
};
export { ImgButton };
After importing this component ImgButton I call it with this block of code:
<ImgButton
onPress={console.log("pressed")}
img={'../img/button_australia.png'}
/>
I get the error: "Requiring unknown module '../img/button_australia.png'"
I assume I've gone wrong when passing the string down as a prop but from the examples I've looked I don't see what's wrong with what I've done. Thanks :)
As discussed in this react-native issue, it's not possible to require assets or javascript modules with dynamically generated names, e.g. variables.
This is because the React Native packager uses require (and import) statements to generate the module and asset bundles at compile-time, so the value of the variable is not known.
The simplest way is to just pass the image source to the component directly:
<ImgButton
onPress={console.log("pressed")}
img={require('../img/button_australia.png')}
/>
Somebody try to use react-native-icons? I follow this steps:
npm install react-native-icons#latest --save
In XCode, in the project navigator right click Libraries ➜ Add Files to [your project's name]
Go to node_modules ➜ react-native-icons➜ ios and add ReactNativeIcons.xcodeproj
Add libReactNativeIcons.a (from 'Products' under ReactNativeIcons.xcodeproj) to your project's Build Phases ➜ Link Binary With Libraries phase
Add the font files you want to use into the Copy Bundle Resources build phase of your project (click the '+' and click 'Add Other...' then choose the font files from node_modules/react-native-icons/ios/Libraries/FontAwesomeKit).
Run your project (Cmd+R)
My Code
var React = require('react-native');
var Icon = require('FAKIconImage');
var { AppRegistry, StyleSheet, Text, View} = React;
class BringgersApp extends React.Component {
constructor(props) {
super(props);
}
render() {
return(
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to Bringgers!
</Text>
<Icon
name='ion|beer'
size={150}
color='#887700'
style={styles.beer} />
</View>
)
}
}
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
React.AppRegistry.registerComponent('BringgersApp', function() { return BringgersApp });
After I build, he says the file doesn't exist...
Font file doesn't exist
I clean the DerivedData and try to build many times, but doesn't work.
Steps to install and usage Ionicons, FontAwesome and Entypo font Icons in react-native.
First you need to install using following command.
react-native install react-native-vector-icons
Then Required to Link:
react-native link react-native-vector-icons
Import font package files to your page:
import Ionicons from 'react-native-vector-icons/Ionicons';
import FontAwesome from 'react-native-vector-icons/FontAwesome';
import Entypo from 'react-native-vector-icons/Entypo';
Usage Example:
<View>
<Text>Ionicons Icons</Text>
<Icon name='md-bicycle' />
<Text>FontAwesome Icons</Text>
<FontAwesome name='trophy' />
<Text>Entypo Icons</Text>
<Entypo name='aircraft' />
</View>
If you want to see list of available icons, you can look in this directory:
Ionicons:
\node_modules\react-native-vector-icons\glyphmaps\Ionicons.json
FontAwesome:
\node_modules\react-native-vector-icons\glyphmaps\FontAwesome.json
Entypo:
\node_modules\react-native-vector-icons\glyphmaps\Entypo.json
Giving you my apps screenshot.
First things first,
the maintainer of react-native-iconshimself points to the newer & maintained react-native-vector-icons
Apparently as of now the rnpm project has been merged into react-native.
In other words, your life can be as easy as typing
react-native install react-native-vector-icons
react-native link react-native-vector-icons
And you may be good to go*)
*) at least it worked on my machine
Did you import file node_modules/react-native-icons/ios/ReactNativeIcons/Libraries/FontAwesomeKit/ionicons.ttf to your project?
Use React Native Package Manager : https://github.com/rnpm/rnpm
$ npm install rnpm -g
Running
Installing dependency:
If you want to install a dependency and link it in one run:
$ rnpm install react-native-icons
Linking dependency:
If you already have some installed (but not linked) modules, run:
$ rnpm link
Do you have "Build settings" -> "Header Search Paths" -> "$(SRCROOT)/node_modules/react-native/React" recursive?
Did you include icon fonts to "Build phases" -> "Copy Bundle Resources" in your Main project?
So in my project, I access the Icons through this code: let { Icon, } = require('react-native-icons');
Although I never had issues using FontAwesome icons, I had issues using Material Icons. You can also check out react-native-vector-icons.
For using icons in react native, there are many libraries available, react-native-vector-icons is one of them, it is very easy to use.
just follow 2 steps, first you have to install library,
then you have to link it also, to use it in your project.
Run this command for installing library
npm install react-native-vector-icons --save
Run this command for linking library
react-native link react-native-vector-icons
How to use this library
You can use any directory showed in the image, each directory has a lots of icons in it, now you have to choose which directory has a best icon for you...
For finding best directory for you, I will recommend you, simply visit this link:
Find best icon for your app
On this website simply search the name of icon you are lookig for,
You will get matching icons in all directories, you can simply remember the name of your selected icon, and use it in your app...
suppose I search arrow on this link, and I selected arrowsalt from AntDesign directory and want to use it in my App.
import Icon from 'react-native-vector-icons/AntDesign';
if you want to use FontAwesome, then simply replace AntDesign with FontAwesome, or the name of directory you want to use.
e.g import Icon from 'react-native-vector-icons/DirectoryName';
And use icon like this.
<Icon name="arrowsalt" size={30} color="blue" />
For further details Visit this link
Reference:
https://openbase.com/js/react-native-vector-icons/documentation
https://aboutreact.com/react-native-vector-icons/
https://oblador.github.io/react-native-vector-icons/
try
rnpm link // If you installed module
Also
npm install module-name
and after execute
rnpm link
restart services..
This code relink automatic your modules.
Work to me. Thanks.
you can also try with native-base library.
it provide tag
it is easy and simple to use any icon which can be use with mobile devices.
follow this link for more information on native base icon.
http://nativebase.io/docs/v2.0.0/components#icon
follow this link for more information on list of icon.
https://ionicframework.com/docs/v2/ionicons/
ex.
<Icon name='ios-list' style={style.icon} />
Step 1: Install react-native-elements
yarn add react-native-elements
# or with npm
npm i react-native-elements --save
Step 2: Install react-native-vector-icons
If you have already installed react-native-vector-icons as a dependency for your project you can skip this step. Otherwise run the following command:
# yarn
yarn add react-native-vector-icons
# or with npm
npm i --save react-native-vector-icons
# link
react-native link react-native-vector-icons
and after this just simply use them in your project like
import { Icon } from 'react-native-elements';
class IonBeer extends React.Component {
render() {
return (
<Icon
name='md-beer'
type='ionicon'
color='#887700'
size=150
/>
);
}
}
and then simply use it like.
<IonBeer/>
any where you want
For Icon you can use react-native vector icon . It comprises of various icon that we can use in your project for a native look .
for more you can have a look https://github.com/oblador/react-native-vector-icons.
For more native components you can have a look at nativebase.io .It's an awesome library for UI in react native .