Module not found: Can't resolve './picker' with React Native DateTimePicker - react-native

I would like to have a date input field in a react-native form and I installed React Native DateTimePicker with the command: expo install #react-native-community/datetimepicker
To get started I used the following component almost exactly as described in the documentation:
import DateTimePicker from '#react-native-community/datetimepicker';
import React, { useState } from 'react';
import { Button, View, Text } from 'react-native';
export default function Picker () {
const [date, setDate] = useState(new Date(1598051730000));
const [mode, setMode] = useState('date');
const [show, setShow] = useState(false);
const onChange = (event, selectedDate) => {
const currentDate = selectedDate;
setShow(false);
setDate(currentDate);
};
const showMode = (currentMode) => {
setShow(true);
setMode(currentMode);
};
const showDatepicker = () => {
showMode('date');
};
const showTimepicker = () => {
showMode('time');
};
return (
<View>
<View>
<Button onPress={showDatepicker} title="Show date picker!" />
</View>
<View>
<Button onPress={showTimepicker} title="Show time picker!" />
</View>
<Text>selected: {date.toLocaleString()}</Text>
{show && (
<DateTimePicker
testID="dateTimePicker"
value={date}
mode={mode}
is24Hour={true}
onChange={onChange}
/>
)}
</View>
);
}
Then imported in another component.
However I get the following error:
./node_modules/#react-native-community/datetimepicker/src/DateTimePickerAndroid.js:23
Module not found: Can't resolve './picker'
21 | validateAndroidProps,
22 | } from './androidUtils';
> 23 | import pickers from './picker';
24 |
25 | function open(props: AndroidNativeProps) {
26 | const {
./node_modules/#react-native-community/datetimepicker/src/androidUtils.js:6
Module not found: Can't resolve './picker'
4 | */
5 | import {ANDROID_DISPLAY, ANDROID_MODE, MIN_MS} from './constants';
> 6 | import pickers from './picker';
7 | import type {AndroidNativeProps, DateTimePickerResult} from './types';
8 | import {sharedPropsValidation} from './utils';
9 | import invariant from 'invariant';

This error shows up when importing this package in the web version of react-native. Since the picker doesn't work on web anyway the easiest workaround is to not import it on web.
The way to do that is to create a separate file for native code. If you have a DateTimePicker.jsx file then rename it to DateTimePicker.native.jsx, then create a new file named DateTimePicker.jsx with the content:
export default function Picker() {
return <Text>Picker does not work on web.</Text>
}
Now you can import DateTimePicker from "DateTimePicker" and the android/ios version will import the picker, while on web you will get the noop version.

Try to back version to 6.0.2
Seems it is the actual issue for some people: https://github.com/react-native-datetimepicker/datetimepicker/issues/626
UPD:
I've tried to run your example and it works. My package.json:
"dependencies": {
"expo": "~45.0.0",
"expo-status-bar": "~1.3.0",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-native": "0.68.2",
"react-native-web": "0.17.7",
"#react-native-community/datetimepicker": "6.1.2"
},
If it won't help, try to reinstall node_modules and clear cash: https://facebook.github.io/metro/docs/troubleshooting/

Related

undefined is not an object _react.PropTypes.array

I am using React Native (Expo CLI). Since i reinstalled npm i have this error when i try to run npm start I found some similar erros related to react-native-snap-carousel (i am using it).
TypeError: undefined is not an object (evaluating '_react.PropTypes.array')
at node_modules\expo\build\environment\react-native-logs.fx.js:27:4 in error
at node_modules\react-native\Libraries\Core\ExceptionsManager.js:95:4 in reportException
at node_modules\react-native\Libraries\Core\ExceptionsManager.js:141:19 in handleException
at node_modules\react-native\Libraries\Core\setUpErrorHandling.js:24:6 in handleError
at node_modules\#react-native\polyfills\error-guard.js:49:36 in ErrorUtils.reportFatalError
at node_modules\metro-runtime\src\polyfills\require.js:203:6 in guardedLoadModule
at http://192.168.1.6:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=android&dev=true&hot=false&strict=false&minify=false:202384:3 in global code
So, inside it's main file is imported {PropTypes} from "react"; and changed to import PropTypes from "prop-types"; (previously installed). Also changed propTypes.items: PopTypes.array.isRequired to propTypes.items:PropTypes.array, same to slideStyle (because I started getting new errors related to items and slideStyle are required) but now i am receiving new errors like this.
All errors are related to react-native-snap-carousel
Here is my package.json
Here is how i use react-native-snap-carousel
import React, { useState, useEffect } from "react";
import {
View,
StyleSheet,
Image,
Dimensions,
TouchableWithoutFeedback,
} from "react-native";
import { getBannersApi } from "../../Api/HomeBanner";
import Carousel, { Pagination } from "react-native-snap-carousel";
import { size } from "lodash";
import { useNavigation } from "#react-navigation/native";
import { SERVER_RESOURCERS } from "../../Utils/Constans";
const width = Dimensions.get("window").width;
const height = 160;
export default function Banner() {
const [banners, setBanners] = useState(null);
const [banneActive, setBanneActive] = useState(0);
const navigation = useNavigation();
useEffect(() => {
(async () => {
const response = await getBannersApi();
setBanners(response.data);
})();
}, []);
const goToProduct = (id) => {
navigation.push("product", { idProduct: id });
};
if (!banners) return null;
const renderItem = ({ item }) => {
return (
<TouchableWithoutFeedback
onPress={() => goToProduct(item.attributes.product.data.id)}
>
<Image
style={styles.carousel}
source={{
uri: `${SERVER_RESOURCERS}${item.attributes.banner.data[0].attributes.formats.small.url}`,
}}
/>
</TouchableWithoutFeedback>
);
};
return (
<View style={styles.container}>
<Carousel
layout="default"
data={banners}
sliderWidth={width}
itemWidth={width}
renderItem={renderItem}
loop={true}
onSnapToItem={(i) => setBanneActive(i)}
autoplay={true}
autoplayInterval={5000}
autoplayDelay={2000}
/>
<Pagination
dotsLength={size(banners)}
activeDotIndex={banneActive}
inactiveDotOpacity={0.6}
inactiveDotScale={0.6}
containerStyle={styles.dotsContainer}
dotStyle={styles.dot}
dotColor={styles.dot.backgroundColor}
inactiveDotColor={styles.dot.backgroundColor}
/>
</View>
);
}
I noticed react-native-snap-carousel is not supported anymore so I migrated to react-native-reanimated-carousel

Native Base: isFocused not working on Input | React Native

The following code:
import { Button, IInputProps, Input } from 'native-base';
const App: React.FC = () => {
const inputRef = useRef<IInputProps>({});
return (
<>
<Button
onPress={() => {
if (inputLocationRef.current) {
inputLocationRef.current.isFocused = true;
}
}}
>
<Text>click</Text>
</Button>
<Input ref={inputLocationRef} />
</>
)
}
is not focusing as expected.
I tried to follow the native-base docs, where they spell out about the isFocused property. However I did not get the expected behavior
When I press: (focus not working)
Expected behavior
Environment:
package.json
"dependencies": {
"react": "18.0.0",
"react-dom": "18.0.0",
"native-base": "^3.4.13",
"expo": "^46.0.7"
}

Trying to play an audio file with react-native

I just started getting my hands on React Native and am currently trying to play an audio file in a test app. But it does not work at this point. Here is what I do:
First setting the app, on the terminal.
$ expo init TestAudio
$ cd TestAudio
$ npm install react-native-sound --save
$ npm run web
$ cp ...../TEST.mp3 .
Then here is how I set up the App.js file:
(Its is mainly inspired from information I gathered from various samples and documents I found on the net, please tell me if something is wrong..)
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Button, View } from 'react-native';
import React, { useEffect } from 'react';
import Sound from 'react-native-sound';
export default function App() {
let control_Local, localSound = require('./TEST. mp3');
useEffect(() => {
Sound.setCategory('Playback', true);
return () => {
if (control_Local) control_Local.release();
};
}, []);
const playSound_Local = () => {
control_Local = new Sound(localSound, (error, _sound) => {
if (error) {
alert('error' + error.message);
return;
}
control_Local.play(() => {
control_Local.release();
});
});
}
return (
<View style={styles.container}>
<Button title='Play-Sound!'
onPress={playSound_Local}
/>
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
Unfortunately it does not work and this is the error message I get in the web console:
./node_modules/react-native/Libraries/Image/AssetSourceResolver.js:24:17
Module not found: Can't resolve '../Utilities/Platform'
22 |
23 | const PixelRatio = require('../Utilities/PixelRatio');
> 24 | const Platform = require('../Utilities/Platform');
| ^
25 |
26 | const invariant = require('invariant');
27 | index.js:1
Searching the net based on the error message, shows that it is not such an uncommon issue, but I didn't see any clear solution.
Has someone had the same problem to solve or has an idea of what to do?
​

How can I use react native datetime picker

As I want to use datepicker I'm getting this error
null is not an object (evaluating '_reactNative.NativeModules.RNDateTimePickerManager.getDefaultDisplayValue')
Source codes
import React, {useState} from 'react';
import DateTimePicker from '#react-native-community/datetimepicker';
const ExpenseStepThree = () => {
const [date, setDate] = useState(new Date());
const onChange = (event, selectedDate) => {
const currentDate = selectedDate || date;
setDate(currentDate);
};
return <DateTimePicker value={date} onChange={onChange} />;
};
export default ExpenseStepThree;
Measure to fix issue I did
pod-install
npm install #react-native-community/datetimepicker --save
Device specification
Xcode 12.5
"react": "17.0.2",
"#react-native-community/datetimepicker": "^3.5.2",
"react-native": "0.66.1",
Try react-native-modal-datetime-picker package instead.

Error taking snapshot after using material-top-tabs because of react-native-reanimated mock

Current Behavior
After using #react-navigation/material-top-tabs my snapshot fails given the following error
TypeError: Cannot read property 'name' of undefined
at Object.onIndexChange (/Users/snowflake/SnowflakeProjects/iknewit-mobile/node_modules/#react-navigation/material-top-tabs/lib/commonjs/views/MaterialTopTabView.tsx:51:9)
at Object.onIndexChange (/Users/snowflake/SnowflakeProjects/iknewit-mobile/node_modules/react-native-tab-view/lib/commonjs/TabView.tsx:84:18)
at b (/Users/snowflake/SnowflakeProjects/iknewit-mobile/node_modules/react-native-tab-view/lib/commonjs/Pager.tsx:565:22)
at call (/Users/snowflake/SnowflakeProjects/iknewit-mobile/node_modules/react-native-reanimated/mock.js:112:21)
at new Pager (/Users/snowflake/SnowflakeProjects/iknewit-mobile/node_modules/react-native-tab-view/lib/commonjs/Pager.tsx:560:7)
at constructClassInstance (/Users/snowflake/SnowflakeProjects/iknewit-mobile/node_modules/react-test-renderer/cjs/react-test-renderer.development.js:3932:18)
at updateClassComponent (/Users/snowflake/SnowflakeProjects/iknewit-mobile/node_modules/react-test-renderer/cjs/react-test-renderer.development.js:7639:5)
at beginWork$1 (/Users/snowflake/SnowflakeProjects/iknewit-mobile/node_modules/react-test-renderer/cjs/react-test-renderer.development.js:9159:16)
at performUnitOfWork (/Users/snowflake/SnowflakeProjects/iknewit-mobile/node_modules/react-test-renderer/cjs/react-test-renderer.development.js:12981:12)
at workLoopSync (/Users/snowflake/SnowflakeProjects/iknewit-mobile/node_modules/react-test-renderer/cjs/react-test-renderer.development.js:12955:22)
Expected Behavior
snapshot get saved
How to reproduce
Messages-test.js:
/** #flow **/
import React from 'react';
import { render } from 'react-native-testing-library';
import { NavigationContainer } from '#react-navigation/native';
import { Messages } from '../Messages';
import { MockedProvider } from '../../core/utils/tests/mockedProvider';
describe('<Messages />', () => {
const { toJSON } = render(
<MockedProvider>
<NavigationContainer>
<Messages />
</NavigationContainer>
</MockedProvider>
);
it('should match <MessagesScreen /> snapshot', () => {
expect(toJSON()).toMatchSnapshot();
});
});
mockedProvider.js:
/** #flow **/
import type { Element } from 'react';
import React from 'react';
import { dark, mapping } from '#eva-design/eva';
import { EvaIconsPack } from '#ui-kitten/eva-icons';
import { MockedProvider as ApolloProvider } from '#apollo/react-testing';
import { ApplicationProvider as UIKittenMockedApplicationProvider, IconRegistry } from '#ui-kitten/components';
import { ThemeProvider, useTheme } from '../../context/themeContext';
type MockedProviderPropsType = {
children: React$Node
};
export function MockedProvider(props: MockedProviderPropsType): Element<any> {
const { children } = props;
const { theme, themeToggle } = useTheme();
return (
<ThemeProvider value={{ theme, themeToggle }}>
<IconRegistry icons={EvaIconsPack} />
<UIKittenMockedApplicationProvider mapping={mapping} theme={dark}>
<ApolloProvider>{children}</ApolloProvider>
</UIKittenMockedApplicationProvider>
</ThemeProvider>
);
}
Messages.js:
/** #flow **/
import React from 'react';
import { StyleSheet } from 'react-native';
import { Layout } from '#ui-kitten/components';
import { TabNavigator } from '../core/navigations/topTabNavigator';
export function Messages(): React$Element<any> {
return (
<Layout level='3' style={style.container}>
<TabNavigator />
</Layout>
);
}
const style = StyleSheet.create({
container: {
flex: 1
}
});
topTabNavigator.js:
/** #flow **/
import React from 'react';
import { SafeAreaView, StyleSheet } from 'react-native';
import { createMaterialTopTabNavigator } from '#react-navigation/material-top-tabs';
import { Icon, Tab, TabBar } from '#ui-kitten/components';
import { MessagesList } from '../../components/MessagesList';
const TopTabBar = ({ navigation, state }) => {
const onSelect = (index) => {
navigation.navigate(state.routeNames[index]);
};
const tabIcon = (style, icon): React$Element<any> => <Icon {...style} name={icon} />;
return (
<TabBar selectedIndex={state.index} onSelect={onSelect}>
<Tab title='Received' icon={(style) => tabIcon(style, 'inbox')} />
<Tab title='Sent' icon={(style) => tabIcon(style, 'inbox-outline')} />
</TabBar>
);
};
export const TabNavigator = (): React$Element<any> => {
const TopTab = createMaterialTopTabNavigator();
const style = StyleSheet.create({ container: { flex: 1 } });
return (
<SafeAreaView style={style.container}>
<TopTab.Navigator tabBar={(props) => <TopTabBar {...props} />}>
<TopTab.Screen name='Received' component={MessagesList} />
<TopTab.Screen name='Sent' component={MessagesList} />
</TopTab.Navigator>
</SafeAreaView>
);
};
jestSetupFile.js:
/**
* Jest configuration
* #flow
**/
jest.mock('#react-native-community/async-storage');
jest.mock('react-native-reanimated', () => require('react-native-reanimated/mock'));
jest.mock('react-native/Libraries/Animated/src/NativeAnimatedHelper');
Your Environment
| software | version |
| ----------------------------------- | ------- |
| #react-navigation/native | 5.0.5 |
| #react-navigation/material-top-tabs | 5.0.7 |
| react-native-tab-view | 2.13.0 |
| react-native | 0.61.5 |
| node | 12.16.0 |
| yarn | 1.22.0 |