Undefined is not an object(evaluating '_expo.default.Constants') - react-native

I am getting an error at Constants in const{manifest}=Expo.Constants and showing me an error as undefined is not an object. I am not able to get it. Can anyone help me in removing that error.
I am trying to make a connection between API running on my computer and when I am debugging on my phone. it should connect regardless of what the IP address is.
import moment from 'moment';
import Expo from 'expo';
const{ manifest} = Expo.Constants;
const api = manifest.packagerOpts.dev
? manifest.debuggerHost.split(':').shift().concat(':3000')
: 'productionurl.com'
const url ='http://localhost:3000';
export function getEvents(){
return fetch(url)
.then(response => response.json())
.then(events => events.map( e =>({...e})));
}
export function formatDateTime(dateString){
const parsed = moment(new Date(dateString));
if(!parsed.isValid()){
return dateString;
}
return parsed.format('H A on DD MMM YYYY');
}
export function formatDate(dateString){
const parsed = moment(new Date(dateString));
if(!parsed.isValid()){
return dateString;
}
return parsed.format('DD MMM YYYY');
}
export function getCountdownParts(eventDate){
const duration = moment.duration(moment(new Date(eventDate)).diff(new Date()));
return{
days:parseInt(duration.as('days')),
};
}
Expected events to be displayed on my emulator from db.json file but
I am getting error as Undefined.

The following works for me:
Instead of
import Expo from 'expo';
and (for example):
const { version } = Expo.Constants.manifest;
Which gave the above error,
I wrote:
import Constants from 'expo-constants';
and:
const { version } = Constants.manifest;

I had the same issue with the error:
undefine is not an object (evaluating _expoLocation.default.reverseGeocodeAsync)
I solved it with an import change.
Before:
import Location from "expo-location";
After:
import * as Location from "expo-location";

you can try this code.
import { Constants as Manifest } from 'expo';
Read how to use it for a detailed description.

Related

React-Native retrieve API_URL from AsyncStorage and make in accessible in all app (for test purpose)

I am trying to retrieve the API_URL from AsyncStorage and make it accessible in all app, the storing and retrieving (in settings screen) is working fine but when I try to load the data in the App.js using useEffect hook, it returns null. Reloading the app is not working but as soon as I save the App.js (using CTRL-S) it works fine.
Please let me know the correct way to do this.
import React, { useEffect, useState } from "react";
import AsyncStorage from "#react-native-async-storage/async-storage";
export default function App() {
const [hostState, setHostState] = useState(null);
const getAHostInfoAsync = async () => {
const hostInfo = AsyncStorage.getItem('host').then(
setHostState(hostInfo)
).then(
console.log(hostState)
);
};
useEffect(() => {
getAHostInfoAsync();
}, []);
module.exports = {
host: hostState
};
}
and using in another file:
import App from "../../../App";
const API_URL = App.host;
I think your issue is in the way you use async/then. instead of async await.
I am not 100% sure that this is your issue. But if I change my async/await function to use async/then the way you are having it, my IDE says that the variable (hostInfo) might not have been initialised. In any case, I think this is a better syntax than with then.
const getAHostInfoAsync = async () => {
const hostInfo = await AsyncStorage.getItem('host')
setHostState(hostInfo)
console.log(hostState)
};

global.__reanimatedWorkletInit is not a function. react-native-animated v2

I using 'worklet'; and runOnUI() and then get the error below.
Because I use 'worklet'; I added import 'react-native-reanimated'
Because I also use runOnUI I added import {runOnUI} from 'react-native-animated'
Error:
ERROR TypeError: global.__reanimatedWorkletInit is not a function. (In 'global.__reanimatedWorkletInit(_f)', 'global.__reanimatedWorkletInit' is undefined)
ERROR Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication). A frequent cause of the error is that the application entry file path is incorrect.
This can also happen when the JS bundle is corrupt or there is an early initialization error when loading React Native.
ERROR Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication). A frequent cause of the error is that the application entry file path is incorrect.
This can also happen when the JS bundle is corrupt or there is an early initialization error when loading React Native.
Short answer:
Besides runOnUI also import and use something else from react-native-reanimated, for example also add useSharedValue:
import { useSharedValue, runOnUI } from 'react-native-reanimated';
and use the shared value:
const sharedVal = useSharedValue(0);
Now the error will go away.
Long answer (how I got to the solution):
At first I got the error (global.__reanimatedWorkletInit is not a function.) when trying to create a widget:
import React from 'react';
import { Text } from 'react-native';
const Example = () => {
const someWorklet = () => {
'worklet';
console.log("Hey I'm running on the UI thread");
};
return (<Text>Example</Text>);
};
export default Example;
Adding import 'react-native-reanimated'; helped me to get rid of the error.
Then I wanted to run someWorklet. So I added a runOnJS (runOnUI got me the same results) call. My component now returned:
<View>
<Text>Worklet: {runOnUI(someWorklet)()}</Text>
</View>
and I imported it via import { runOnUI } from 'react-native-reanimated';.
Now the same error was back.
Only after I also imported and called useSharedValue everything worked as expected. This is my component code which didn't throw the error anymore:
import React from 'react';
import { View, Text } from 'react-native';
import { useSharedValue, runOnJS } from 'react-native-reanimated';
const Example = () => {
const someWorklet = () => {
'worklet';
console.log("Hey I'm running on the UI thread");
return 'World';
};
const sharedVal = useSharedValue(0);
return (
<View>
<Text>Hello, {runOnJS(someWorklet)()}</Text>
</View>
);
};
export default Example;
Just add these two lines in App.tsx and index.js
1)import "react-native-reanimated"
2)global.__reanimatedWorkletInit = () => {}

react-redux useSelector() hook not working

I am new to React Native Programming. So, please tell me in detail. thank you.
calling use Selector
I am calling use Selector inside my functional component like this:
import { useDispatch, useSelector } from 'react-redux';
const AddAddressScreen = ({ navigation }) => {
const dispatch = useDispatch();
const data = useSelector(state => state);
console.log(data + "happy Coding");
return (
<View style={styles.container}>
<View>
);
}
export default AddAddressScreen;
My reducer looks like this
case types.API_LOGIN_SUCCESS:
if (action.result.result.mobile_verified === false) {
return {
...state,
onLoad: false,
result: action.result,
status: action.status,
error: null,
navigation: action.navigation.navigate("VerifyMNO")
};
} else {
return {
...state,
onLoad: false,
result: action.result,
status: action.status,
error: null,
navigation: action.navigation.navigate("AddAddress")
};
}
here my mobile number is verified so I move to the address screen.
where I use Use Selector which gives me an error. while I remove above two lines my code runs successfully.
My saga looks like this
export function* watchLoginUserInfo() {
yield takeLatest(types.LOGIN_USER, loginApiSaga)
}
My root saga
import { all, fork } from 'redux-saga/effects';
import { watchLoginUserInfo, } from './authenticationSagas';
function* rootSaga() {
yield all([
watchLoginUserInfo(),
])
}
export default rootSaga;
My Store looks like this
import {createStore, applyMiddleware} from 'redux';
import rootReducer from '../redux/reducers/root-reducer.js'
import createSagaMiddleware from 'redux-saga';
import rootSaga from '../redux/sagas/rootSaga';
const sagaMiddleware = createSagaMiddleware();
const store = createStore(rootReducer, applyMiddleware(sagaMiddleware));
sagaMiddleware.run(rootSaga);
export {store};
when ever I use use Selector hook in my code it gives me the following error.
error 1
error 2, 3, 4
Use the select effect from redux-saga inside of a reducer: https://redux-saga.js.org/docs/api/#selectselector-args
For example const selectedState = yield select(state => state);.
The useSelector hook is for use inside of a function component.
EDIT: since the above doesn't seem to be the issue, I think the issue is that you're calling navigation functions from within your reducer. Reducer code can have no side effects, so you can't call navigation.navigate(...) from within the reducer. This will need to happen in the saga code instead. It might be able to be done in the loginApiSaga or in a dedicated saga that is triggered by API_LOGIN_SUCCESS.

React Native: Function Undefined

I am building an app in react native and keep getting an error with a function being defined and cannot seem to figure out how to fix it.
This is the portion of the code that causes the error. It is meant to fetch data from an api using redux and redux-thunk to do it.
componentDidMount() {
this.props.fetchData(
V.sprintf(
"http://timetree.igem.temple.edu/api/pairwise/%s/%s",
this.state.taxonA,
this.state.taxonB
)
);
}
here is the fetchData function that the component uses it is located in an actions folder.
export const fetchData = url => {
return async dispatch => {
dispatch(fetchingRequest());
try {
let response = await fetch(url);
let json = response.json();
dispatch(fetchingSuccess(json));
} catch(error){
dispatch(fetchingFailure(error));
}
}
};
here is how I try to pass it to the component
SearchScreen.propTypes = {
fetchData: PropTypes.func.isRequired,
response: PropTypes.object.isRequired
};
So as I said in comments with propTypes you're not passing anything, you're testing the type of your props. You should import your function and pass it to connect.
import { connect } from 'react-redux';
import { fetchData } from '../actions';
export default connect(mapStateToProps, { fetchData })(App);
the propType define only the types, not the actual value.
if you export the function
export const fetchData
just import it from your component file, and use it:
fetchData(
V.sprintf(
"http://timetree.igem.temple.edu/api/pairwise/%s/%s",
this.state.taxonA,
this.state.taxonB
)
);
without "this.props".

Can not read property length of undefined in React-Native/VirtualizedSectionList.js

I was trying to modify the state of my react-native app; however, after the modification I get the following error message.
Error Message
I have stack traced it to line 230 in the VirtualizedSectionList of react native. I am not sure what exactly would be causing the error. A potential solution may be resetting my react-native store, but I could not figure out how to do that. Here is my current redux store code.
'use strict';
import {applyMiddleware, createStore} from 'redux';
import thunk from 'redux-thunk';
import {persistStore, autoRehydrate} from 'redux-persist';
import {AsyncStorage} from 'react-native';
import reducers from '../reducers';
const logger = store => next => action => {
if(typeof action === 'function') console.log('dispatching a function');
else console.log('dispatching', action);
let result = next(action);
console.log('next state', store.getState());
return result;
}
let middlewares = [
logger,
thunk
];
let createAppStore = applyMiddleware(...middlewares)(createStore);
export default function configureStore(onComplete: ()=>void){
const store = autoRehydrate()(createAppStore)(reducers);
let opt = {
storage: AsyncStorage,
transform: [],
blacklist: ['nav','util']
};
persistStore(store, opt, onComplete);
return store;
}
Thanks
This doesn't seem an issue with store, i think it is there where you are using ur states may be or something else, if you upload other code than i can check it but i don't think this error is due to store.