Error: Requiring module "node_modules\react-native-reanimated\src\Animated.js", - react-native

I am trying to use createDrawerNavigator from import { createDrawerNavigator } from '#react-navigation/drawer'; in react native. However, I am getting the error below, which I don't know how to solve.
Error: Requiring module
"node_modules\react-native-reanimated\src\Animated.js", which threw an
exception: Error: Reanimated 2 failed to create a worklet, maybe you
forgot to add Reanimated's babel plugin?
In babel.config.js I tried to add the below code but not working as well
module.exports = function(api) {
api.cache(true);
return {
presets: ['babel-preset-expo'],
plugins: [
'react-native-reanimated/plugin',
]
};
};
The Below code is my component
import * as React from 'react';
import { Button, View } from 'react-native';
import { createDrawerNavigator } from '#react-navigation/drawer';
import { NavigationContainer } from '#react-navigation/native';
function HomeScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button
onPress={() => navigation.navigate('Notifications')}
title="Go to notifications"
/>
</View>
);
}
function NotificationsScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button onPress={() => navigation.goBack()} title="Go back home" />
</View>
);
}
const Drawer = createDrawerNavigator();
export default function MyDrawer() {
return (
<NavigationContainer>
<Drawer.Navigator initialRouteName="Home">
<Drawer.Screen name="Home" component={HomeScreen} />
<Drawer.Screen name="Notifications" component={NotificationsScreen} />
</Drawer.Navigator>
</NavigationContainer>
);
}

Please complete the setup for react-native-reanimated.
You have to add 'react-native-reanimated/plugin', in the babel.config.js file so the final code in babel.config.js will look like
module.exports = {
...
plugins: [
...
'react-native-reanimated/plugin',
],
};
As state in the setup docs for react-native-reanimatedHere
Also you need to complete setup for android as well (if not done yet) as stated Here
If you are using expo then follow these steps
Finally, run expo r -c to clear the cache.

If you are using expo. Set this in babel.config.js:
module.exports = function(api) {
api.cache(true);
return {
presets: ['babel-preset-expo'],
plugins: ['react-native-reanimated/plugin'],
};
};
Note: If you load other Babel plugins, the Reanimated plugin has to be the last item in the plugins array
After you add the Babel plugin, restart your development server and clear the bundler cache: expo start --clear.

You must install according to these instructions:
[https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/installation/][1]
Also, make no mistake Be careful where you write the following code
#Override
protected JSIModulePackage getJSIModulePackage() {
return new ReanimatedJSIModulePackage(); }

[Solution][1]
yarn add react-native-reanimated
cd ios
pod install
And Import on either Index or App .js file
import Animated from 'react-native-reanimated';
[1]: https://docs.swmansion.com/react-native-reanimated/docs/1.x.x/getting_started/

I am using react-native 0.69.3 and also had an issue with compiling reanimated#2.10. I updated to #2.11.0 and it fixed my compile error.

Make sure react-native-reanimated is up to date and the other packages are the same then try running npx react-native link react-native-reanimated.
If that didn't work you need to set up react-native-reanimated properly. Check out the documentation on how to set it up.

According to react navigations drawer documentation.
After installing all the packages
To finalize installation of react-native-gesture-handler, add the following at the top (make sure it's at the top and there's nothing else before it) of your entry file, such as index.js or App.js:
import 'react-native-gesture-handler';
Now this might now work for you. To complete the installation you have to change your babel.config.js file add the following:
plugins: [...,"react-native-reanimated/plugin"]
Note ... just means your other plugins if you have. Remove it if you don't have any.
You are almost there. The final thing you have to know and do is:
Note: If you load other Babel plugins, the Reanimated plugin has to be
the last item in the plugins array.
After you add the Babel plugin, restart your development server and
clear the bundler cache: expo start --clear
You can reference the expo reanimated docs for more details

Add this to babel.config.js file:
module.exports = function (api) {
api.cache(true);
return {
presets: ['babel-preset-expo'],
plugins: ['react-native-reanimated/plugin'],
};
then run this to clear cache and restart development server:
npx expo start --clear
learn more on expo official doc.

Related

How to apply google fonts to entire Expo/RN app?

I am trying to use Google Fonts with my entire expo project, but i dont want to import it to each and every component. How can I just import it once, and call it everywhere? Or better yet, can I set my imported font as a default font?
app.js
import { Provider } from 'react-redux'
import store from './redux/store'
import { NavigationContainer } from '#react-navigation/native';
import { createNativeStackNavigator } from '#react-navigation/native-stack';
import { useFonts, Montserrat_400Regular,} from '#expo-google-fonts/montserrat';
import { HomeScreen } from './components/HomeScreen';
import { AddItemScreen } from './components/AddItemScreen';
import { DetailedDayView } from './components/DetailedDayView';
export default function App(navigation) {
const Stack = createNativeStackNavigator()
let [fontsLoaded] = useFonts({
Montserrat_400Regular,
});
if (!fontsLoaded) {
return null;
}
return (
<Provider store={store}>
{/* <SafeAreaProvider> */}
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen
name="Home"
component={HomeScreen}
/>
<Stack.Screen
name="AddItemScreen"
component={AddItemScreen}
/>
<Stack.Screen
name="DetailedDayView"
component={DetailedDayView}
/>
</Stack.Navigator>
</NavigationContainer>
{/* </SafeAreaProvider> */}
</Provider>
);
}
For React Native CLI:
First of all, download any fonts in .ttf format.
Create a directory and name it assets, inside the assets directory create another directory name it fonts, and then move the .ttf file here. Your path will look like this: - root/assets/fonts/your-font.ttf
Now, in your root directory create a file named, react-native.config.js. Inside that file paste the following code:
module.exports = {
assets: ['./src/assets/fonts'],
}
Run this command npx react-native-asset. For the older version of React Native try this command npx react-native link
There you go. You're done. Now, check this directory root/android/app/src/main/assets/fonts to check if the font is added or not.
Now you can use your font anywhere in the app. Like this: -
fontFamily: 'your-font'
Note: You can add multiple font-family. For adding another font-family repeat 1, 2, and 4.
For Expo:
For Expo follow this answer: How to apply custom fonts to whole project, Expo React Native

Why does useQuery not work but client.query does?

I am struggling to get ApolloClient useQuery to work in a simple react native application. Using Flipper, I can see that the useQuery does not even create a hit on the graphql endpoint. In debugging this, I tried to use ApolloClient's client.query as an alternative means of making the graphql query and it worked! So I am confused about why useQuery is not working.
Here is the entire App.js code that shows client.query working but useQuery failing to fetch. And a screenshot of the resulting app. I'd really appreciate if someone could tell me where I'm going wrong.
import React, {useState} from 'react';
import {Text, View} from 'react-native';
import {ApolloClient,InMemoryCache,ApolloProvider,gql,useQuery} from '#apollo/client';
const client = new ApolloClient({
uri: 'http://localhost:8000/graphql',
cache: new InMemoryCache(),
});
const ALL_AREAS = gql`
query AllAreas {
allAreas {
id
name
}
}
`;
// Using client.query to make graphql query
function Areas1() {
const [data, setData] = useState('Loading ...');
client
.query({
query: ALL_AREAS,
})
.then(result => {
console.log('Areas 1 Data: ', result.data);
setData('Data ...');
});
return <Text>{data}</Text>;
}
// Using react-hook to make graphql query
function Areas2() {
const {loading, error, data} = useQuery(ALL_AREAS);
if (loading) {
return <Text>Loading ...</Text>;
}
if (data) {
console.log('Areas 2 Data: ', data);
return <Text>Data ...</Text>;
}
}
const App = () => {
return (
<ApolloProvider client={client}>
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
<Text>Hello World {'\n'}</Text>
<Text>
client.query: <Areas1 />
</Text>
<Text>
useQuery: <Areas2 />
</Text>
</View>
</ApolloProvider>
);
};
export default App;
Note: I included console log statements and noticed that the console log from the Areas2 component never fires, whereas the console log from Areas1 does fire and shows the fetched data.
The resulting app in my emulator looks like this:
I am using the following package versions:
"#apollo/client": "^3.6.2",
"graphql": "^16.5.0",
"react": "17.0.2",
"react-native": "0.68.2"
I had some issue with #apollo/client v3.6.2 and have been trying to find the solution. But now you can check on #apollo/client (v3.6.4) for a new realise which is solving the query issue.
Uninstall #apollo/client 3.6.2
npm uninstall #apollo/client or yarn remove #apollo/client
Now install the new realise #apollo/client 3.6.4
npm install #apollo/client or yarn add #apollo/client
Then stop the metro bundler and run the app again
ios: npm run ios or yarn ios
android: npm run android or yarn android

Unable to resolve "./rules/FieldsOnCorrectType" from "node_modules/graphql/validation/index.js"

I'm building a react native application with apollo client 3, and keep getting the following error when generating the javascript bundle.
Failed building JavaScript bundle.
Unable to resolve "./rules/FieldsOnCorrectType" from "node_modules/graphql/validation/index.js"
My code is pretty simple, this is App.tsx:
import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import useCachedResources from './hooks/useCachedResources';
import useColorScheme from './hooks/useColorScheme';
import Navigation from './navigation';
import { ApolloClient, InMemoryCache, ApolloProvider } from '#apollo/client';
const client = new ApolloClient({
uri: 'localhost:4000/graphql',
cache: new InMemoryCache()
});
export default function App() {
const isLoadingComplete = useCachedResources();
const colorScheme = useColorScheme();
if (!isLoadingComplete) {
return null;
} else {
return (
<ApolloProvider client={client}>
<SafeAreaProvider>
<Navigation colorScheme={colorScheme} />
<StatusBar />
</SafeAreaProvider>
</ApolloProvider>
);
}
}
I've tracked it down to the instantiation of the new ApolloClient object - commenting out those lines (and the provider) causes the error to disappear.
Renaming node_modules/graphql/validation/rules/FieldsOnCorrectTypeRule.js to node_modules/graphql/validation/rules/FieldsOnCorrectType.js (dropping the Rule suffix of filename) fixes that specific error, but then errors on the next import in the validation/index.js file... I don't understand why.
I had the same problem with a react native app after upgrading to apollo client 3. For me it was a react native caching issue and the problem went away after following these instructions to clear the cache: How to clear react-native cache?.
I am using the react native cli and used these commands (I didn't check if all of them are actually necessary):
watchman watch-del-all
rm -rf /tmp/metro-cache
rm -rf node_modules
npm cache clean --force
npm install
npm start -- --reset-cache
If you are using the expo cli then apparently you can just do this:
expo start -c

"ARTShape" was not found in the UIManager - React Native

Im using React native Expo(36.0.0).
I am developing dashboard screen for my project. So, I just want to show some pie charts in dashboard screen. I have tried many libraries for all kind of charts. Nothing worked for me. Finally I have used react-native-pie with the help of youtube video. Still it is showing me the same error.
And I am new to React native.
Thanks!
import React, { Component } from 'react';
import { View, Text, ScrollView, StyleSheet, TouchableOpacity, ActivityIndicator, Dimensions } from 'react-native';
import Icon from 'react-native-vector-icons/Ionicons';
import Pie from 'react-native-pie';
class HomeScreen extends Component
{
constructor(props)
{
super(props);
}
render()
{
return(
<ScrollView>
<View style={styles.homeContainer}>
<View style={{paddingVertical:15, flexDirection:'row', width:350, justifyContent:'center'}}>
<Pie
radius={80}
sections={[
{
percentage:10,
color:'red'
},
{
percentage:30,
color:'green'
},
{
percentage:60,
color:'orange'
}
]}
/>
</View>
</View>
</ScrollView>
)
}
}
const styles = StyleSheet.create({
homeContainer:
{
alignItems:'center',
justifyContent:'center'
},
})
export default HomeScreen;
I think, this is not added in RN 0.62 with auto linking feature, so you can do it like this:
go to your ios folder and your project and click on podfile and add the below line with the other similar pods or add it to the end of the podfile.
pod 'ReactNativeART', :path => '../node_modules/#react-native-community/art'
after that go to your editor terminal open your project from the root folder and then:
cd ios
pod install
cd ..
react-native run-ios
Fix for Android
Step 1
Add this lines to android/settings.gradle
include ':react-native-art'
project(':react-native-art').projectDir = new File(rootProject.projectDir, '../node_modules/#react-native-community/art/android')
Step 2
Add this lines to android/app/build.gradle
dependencies {
...
implementation project(':react-native-art')
}
Step 3
Add this lines to android/app/src/main/.../MainApplication.java
import com.reactnativecommunity.art.ARTPackage;
...
#Override
protected List<ReactPackage> getPackages() {
#SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
...
packages.add(new ARTPackage());
...
return packages;
}
reference : https://www.npmjs.com/package/#react-native-community/art
Thx for help unknown asian guy https://bocoder.tistory.com/m/40
npm install #react-native-community/art --save
run the code below.
~$ npm install #react-native-community/art --save
For react-native < 0.60 you need to link ReactNative ART:
~$ react-native link #react-native-community/art
For react-native >= 0.60 ReactNativeART should be autolinked and no additional action is required.

Null is not an object while trying to import from react-native-appearance in expo

I just updated expo to SDK 35 because I wanted to use the new react-native-appearance module to make the dark and light mode dependent of the systems settings. I followed the steps from the docs carefully. (https://docs.expo.io/versions/latest/sdk/appearance/) But if I try to import anything my iPhone 11 Pro Max Emulator gives me the following warning:
null is not an object (evaluating 'NativeAppearance.initialPreferences')
<unknown>
polyfill.tsx:15:68
loadModuleImplementation
require.js:331:6
<unknown>
index.tsx:9:24
loadModuleImplementation
require.js:331:6
<unknown>
App.js:4
loadModuleImplementation
require.js:331:6
<unknown>
AppEntry.js:4
loadModuleImplementation
require.js:331:6
guardedLoadModule
require.js:197:45
global code
<unknown file>:0
Any ideas on how to fix this? Haven't found anything online since this is a pretty new feature.
You probably need to link your module.
On iOS that looks like this:
$ npx pod-install
$ react-native run-ios
not supporting react-native-appearance in expo.
you can use colorscheme and appearance from react native
app.json configuration:
{
"expo": {
"userInterfaceStyle": "automatic"
}
}
In EAS Build and custom development builds you'll need to install the native module expo-system-ui otherwise the userInterfaceStyle property will be ignored. Running expo config --type introspect will warn if the project is misconfigured:
ยป android: userInterfaceStyle: Install expo-system-ui in your project to enable this feature.
Bare projects
iOS configuration
You can configure supported styles with the UIUserInterfaceStyle key in your app Info.plist. Use Automatic to support both light and dark modes.
Android configuration
Appearance locking requires react-native#0.63.3 to work correctly.
Ensure that the uiMode flag is present on your MainActivity (and any other activities where this behavior is desired) in AndroidManifest.xml:
<activity
...
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode">
Implement the onConfigurationChanged method in MainActivity.java (react-native#0.63.3 don't need this):
import React from 'react';
import { Text, StyleSheet, View, Appearance,useColorScheme } from 'react-native';
import { StatusBar } from 'expo-status-bar';
import { DefaultTheme, DarkTheme } from '#react-navigation/native';// automatically switches bar style based on theme!
export default function App() {
const colorScheme = useColorScheme(); or, const colorScheme = Appearance.getColorScheme();
console.log(colorScheme)
const themeTextStyle = colorScheme === 'light' ? styles.lightThemeText : styles.darkThemeText;
const themeContainerStyle =
colorScheme === 'light' ? styles.lightContainer : styles.darkContainer;
return (
<View style={[styles.container, themeContainerStyle]}>
<Text style={[styles.text, themeTextStyle]}>Color scheme: {colorScheme}</Text>
<StatusBar />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
lightContainer: {
backgroundColor: '#d0d0c0',
},
darkContainer: {
backgroundColor: '#242c40',
},
lightThemeText: {
color: '#242c40',
},
darkThemeText: {
color: '#d0d0c0',
},
text:{
color:'#fff',
fontSize:22,
fontWeight:"bold",
},
})