react native vector icons shows X instead of icon - react-native

I've tried different icons, however it seems all of them just show an X instead of the actual icon. There aren't any error messages, here is the code I've used:
import Icon from 'react-native-vector-icons/AntDesign';
<Icon name="heart" size={30} color="red" />
In this case, it should show a heart, but it just appears with an X with a box around it.
This post seems to have a solution, but I do not understand what they did to fix it.
react native vector icons showing X instead of icon
Anyone know how to solve this?

Found that you are using an Android emulator. In the past, I had the same issue many told me to link the project to solve the issue but RN v0.60 and above supports auto-linking, so it is not advised to use manual linking.
npx react-native link -- Avoid this for projects above RN v0.60
Automatic way to add fonts
Add this piece of code in android\app\build.gradle. I only want MaterialCommunityIcons.ttf if you want other font add them here it will be added automatically.
project.ext.vectoricons = [
iconFontNames: ['MaterialCommunityIcons.ttf'] // Add fonts needed
]
apply from: "../../node_modules/react-native-vector-icons/fonts.gradle"
This is a manual way (If automatic didn't work)
Step 1
Move the .ttf file from the node_modules\react-native-vector-icons\Fonts that you want to android\app\src\main\assets\fonts after that cd android and run ./gradlew clean or gradlew clean
Step 2
Add this line in android\app\build.gradle
project.ext.vectoricons = [
iconFontNames: ['MaterialCommunityIcons.ttf', 'AnotherFont.ttf'] //name of all the fonts added from node_modules in step 1.
]
apply from: "../../node_modules/react-native-vector-icons/fonts.gradle"
After that the icons will be visible. Try resetting the cache if it did not work yarn react-native start --reset-cache & yarn react-native run-android
https://github.com/oblador/react-native-vector-icons/issues/1117#issuecomment-589958315
Troubleshooting step from the library if still the above did not work you can try this.
https://github.com/oblador/react-native-vector-icons#the-icons-show-up-as-a-crossed-out-box-on-android

You forgot to load your icon...
put this after your import:
import Icon from 'react-native-vector-icons/AntDesign';
Icon.loadFont();
Cheers!

Here is a complete solution for this. copy the fonts folder from
node_modules\react-native-vector-icons\Fonts
to
android\app\src\main\assets\fonts
you can create the folder there it does not exist by default. then create a file in the root of your project
react-native.config.js
and paste this code.
module.exports = {
project: {
ios: {},
android: {}, // grouped into "project"
},
assets: ["./assets/fonts/"], // stays the same
};

Related

React native vector icon not showing

I am using react native version 0.67.3
I install vector icon and i add andriod/app/build.gradle
apply from: "../../node_modules/react-native-vector-icons/fonts.gradle"
but icon not showing
Use the latest version
"react-native-vector-icons": "^9.1.0",
You can add latest version of it by below command:
npm install react-native-vector-icons#latest
For Android,
write below line in your android -> app -> build.gradle
project.ext.vectoricons = [
iconFontNames: [ 'MaterialIcons.ttf', 'FontAwesome.ttf', 'MaterialCommunityIcons.ttf', 'Ionicons.ttf'] // Name of the font files you want to copy
]
apply from: "../../node_modules/react-native-vector-icons/fonts.gradle"
For iOS,
cd ios & pod install
Use
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
Note: Give exact icon name from https://oblador.github.io/react-native-vector-icons/
I have same problem but when I use
<Text><Ionicons/><Text>
Icons will show in navigation bar.

TypeError: undefined is not an object (evaluating '_reactNative.Animated.Text.propTypes.style')

After upgrading React Native to 0.62.2, and installing react-native-material-dropdown` library, project is giving this error:
I solved this by,
Commenting itemTextStyle: Text.propTypes.style in
..\node_modules\react-native-material-dropdown\src\components\dropdown
file.
And remove Animated in Animated.Text.propTypes.style in
affix/index
helper/index
label/index
of react-native-material-textfield.
And added import { Animated, Text} from 'react-native'; in each of
above three files.
Here is another solution I've found.
Remove installed package react-native-material-dropdown
yarn remove react-native-material-dropdown
Install new packages react-native-material-dropdown-v2 and react-native-paper
yarn add react-native-material-dropdown-v2 react-native-paper
Swap react-native-material-dropdown to react-native-material-dropdown-v2 in your code
e.g.
import { Dropdown } from 'react-native-material-dropdown' to import { Dropdown } from 'react-native-material-dropdown-v2'
I found the same problem while using #react-navigation/drawer
I've solved it by these steps.
Open node_modules and then search for react-native-material-textfield open the file and go to src folder
Under src you will see affix, helper, label folder - under each folder, there is an index.js
open the index.js of the mentioned folders one by one (all 3 folders) and search for the text style: Animated.Text.propTypes.style, and replace it by style: Text.propTypes
And import text form react-native like this import { Animated , Text} from 'react-native';
And now reload the terminal, and you are good to go
There is an issue open on github about this problem. As mentioned in the comment, it is possible to use this option to edit node modules, or create a patch so that it is not necessary to edit the files every time you add a new library or run an npm install.
Instruction:
Create patches directory in your project's root
Copy patch to
patches/react-native-material-textfield+0.16.1.patch
yarn add patch-package postinstall-postinstall or npm i patch-package
yarn patch-package or npx patch-package
Fix is already applied. Add the following to package.json to not repeat the same next time:
"scripts": {
+ "postinstall": "patch-package"
}
https://github.com/n4kz/react-native-material-textfield/issues/249#issuecomment-625791243
I faced the same issue while using react-native-material-dropdown.
Fix:
navigate to node_modules/react-native-material-textfield/src/components
Open files affix/index.js, helper/index.js and label/index.js
Replace style: Animated.Text.propTypes.style with style: Text.propType
import {Text} in each of these 3 files import { Animated ,Text} from 'react-native'
This should fix the issue
react-native version: 0.64.0
Just update the library they updated their library with fixes here is the link
https://www.npmjs.com/package/react-native-material-dropdown-v2-fixed
For me it was the package "react-native-easy-toast" at version 2.0.0. The weird thing was, that I couldn't find "propTypes" or "Animated" anywhere in my Code or in my libs (node-modules). I'd expect to find it somewhere in the react-native-easy-toast folder in node_modules...
Anyway, after commenting all my toasts the app started again.
I now also found a patch for this: "https://github.com/crazycodeboy/react-native-easy-toast/issues/118" and with this and from other here mentioned patch-package it worked with the toasts and the patch gets automatically applied after npm install:)
I am using react-native-material-textfield package, and I also faced this error so I added this piece of code in my JS file right before importing:
import { Animated, Text } from 'react-native';
Animated.Text.propTypes = Animated.Text.propTypes || Text.propTypes;
import { TextField } from 'react-native-material-textfield';
I have faced the same issue while using react-native-material-dropdown.
Fixed using this:
navigate to node_modules/react-native-material-textfield/src/components
Open files affix/index.js, helper/index.js and label/index.js
I have resolved my issue by using this lib
react-native-material-dropdown-no-proptypes

where to put my icon picture for expo to use instead of default one?

according to expo documentation:
The most straightforward way to provide an icon for your app is to provide the icon key in app.json
so I added my own icon to the assets folder where expo's default icons are, and modified app.json to point to my icon:
"expo": {
...
"icon": "./assets/myIcon.png"
}
but when launching I get this error from expo:
Field: icon - cannot access file at './assets/myIcon.png
what am I missing? where should I put my Icon picture?
Solved
by running expo start -c to clear cache.
*Note: it took me a few minutes to sync with my expo client app!
Based on the conversation that I had with the question poster (#Blue Turtle) there are some take aways from this.
Image sizes
It is important to make sure that the image that you are using for your icon must be a perfect square. If it is not Expo will give an error similar to below
Error: Problems validating asset fields in app.json. See https://docs.expo.io/
• Field: icon - image should be square, but the file at './assets/icon.png' has dimensions 1242x2436.
The documents recommend that you use an image that has size 1024x1024 https://docs.expo.io/versions/latest/workflow/configuration/#icon
Local path or remote url to an image to use for your app's icon. We
recommend that you use a 1024x1024 png file. This icon will appear on
the home screen and within the Expo app.
Clearing the cache
Also when updating assets etc, it is advisable to close and restart your bundler, restarting using the following command expo start -c. Starting the bundler this way will ensure that your cache is cleared and that any changes that you have made to packages, assets etc will be made and you will hopefully experience less errors.

ERROR: In <declare-styleable> FontFamilyFont, unable to find attribute android:font React-native, Android

This happened after I've added custom fonts on iOS to assets, added
"rnpm": {
"assets": [
"./assets/fonts/"
]
}
to my package.json and ran react-native link.
After that I couldn't build android.
Turns out we I ran react-native link it created copies of some libraries in 3 files:
android/app/build.gradle:
I had implementation project(':react-native-extra-dimensions-android') and it added compile project(':react-native-extra-dimensions-android')
android/app/src/main/java/com/pivot3/MainApplication.ja:
it added second import
import ca.jaysoo.extradimensions.ExtraDimensionsPackage;
and second package to asList
new ExtraDimensionsPackage(),
android/settings.gradle:
it added extra
include ':react-native-extra-dimensions-android'
project(':react-native-extra-dimensions-android').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-extra-dimensions-android/android')
Probably my react-native link script is broken (I didn't change) but in case you have the same error, check these 3 files for duplicate copies.
Removing extra copies fixes the issue.

Upgrade to react-native 0.16 error

I upgraded my app from react-native 0.15 to 0.16 but after that I'm getting an error and I don't know how to solve it.
TypeError:undefined is not an object (evaluating 'GLOBAL.Text={
get defaultProps(){
throw getInvalidGlobalUseError('Text')}}')
In Chrome Debugger:
Uncaught Error: Uncaught TypeError: Cannot set property 'Text' of undefined
Thanks
OBS: I'm running on Android.
I notice that changing app name solves the problem, I'm using Evently as app name today. I tried to recreate my virtual machine but didn't solve it.
In my case, I was able to narrow the cause down to one item in my .babelrc file:
{
"presets": ["es2015"]
}
As soon as I removed that and restarted the packager (making sure to also use the --reset-cache flag), I stopped getting the error.
Update 2:
It looks like React Native is making some changes to their .babelrc in version 0.20.0. So, if you are using that version or newer, you should follow the instructions at: https://github.com/facebook/react-native/tree/master/babel-preset in order to specify your .babelrc settings.
Update:
I've narrowed this down further to transform-es2015-modules-commonjs, which React-Native sets some options on, specifically {"strict": false, "allowTopLevelThis": true}. The es2015 preset does not set this option, and it seems that the React-Native .babelrc does not override it. If you want to use es6 modules and transform them to commonjs, you'll need to put the following in your .babelrc:
{
"plugins": [
["transform-es2015-modules-commonjs", {"strict": false, "allowTopLevelThis": true}]
]
}
Note, Babel 6, which I updated to along with react-native 0.16.0, no longer contains any transforms by default. What I didn't initially realize is that the React-Native packager provides most of the transforms you might ever need (listed in their docs at: https://facebook.github.io/react-native/docs/javascript-environment.html#javascript-syntax-transformers), and I'm thinking that the "es2015" plugin interferes with some of those transformers.
I also tried using "babel-preset-react" (http://babeljs.io/docs/plugins/preset-react/), and that plugin did not seem to cause any errors.
I solve the problem. I think it was because permissions in project folder. I ran chown in my folder to correct the permissions problems and now all are working.
Thanks
In my case the problem was a rogue .babelrc two folders up (my root code folder); I had initiated a yeoman generator to scaffold out a new project using babel-6...accidentally running yeoman from the root code folder. Apparently babel traversed upwards from my project folders until it hit this .babelrc which borked the react-native babel configs...
^ this was originally an edit to my initial answer, which was deleted WHILE I WAS UPDATING IT