Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all 6413 articles
Browse latest View live

createStackNavigator with Expo and Typescript, doesn't render anything

$
0
0

UPDATE:

Git Repo of NotWorkingProject


Trying to use

react-navigation package in React Native(Expo-Typescript)

the same way as we do with regular React Native (Expo - Vanilla JS)

But it doesn't render anything on the screen.


AppNavigator.tsx

import {createAppContainer, createStackNavigator} from 'react-navigation';
import * as React from 'react';
import {StyleSheet, View, Text} from 'react-native';

const styles = StyleSheet.create({
    container: {
        backgroundColor: '#f0f',
        flex: 1
    }
});

const HomeScreen = () => (
    <View style={styles.container}>
        <View>
            <Text>PLEASE PLEASE PLEASE RENDER THIS</Text>
        </View>
    </View>
);

HomeScreen.navigationOptions = {
    title: 'Home'
};

const AppNavigator = createStackNavigator({Home: HomeScreen});

export default createAppContainer(AppNavigator);

App.tsx

import React from 'react';
import { StyleSheet, Text, View, ViewStyle } from 'react-native';
import AppContainer from './navigation/AppNavigator'

const styles = {
  container: {
    flex: 1,
    backgroundColor: 'red',
    alignItems: 'center',
    justifyContent: 'center',
  } as ViewStyle
};


const App = (style:Object): {} => {
  const { container } = styles;
  return (
    <View style={container}>
      <AppContainer/>
    </View>
  );
}

export default  App;

INCASE IF NEEDED:

package.json

{
  dependencies: {
    expo: "^34.0.1",
    react: "16.8.3",
    "react-dom": "^16.8.6",
    "react-native": "https://github.com/expo/react-native/archive/sdk-34.0.0.tar.gz",
    "react-native-gesture-handler": "^1.3.0",
    "react-native-web": "^0.11.4",
    "react-navigation": "^3.11.1"
  },
  devDependencies: {
    "@types/react": "^16.8.23",
    "@types/react-native": "^0.57.65",
    "babel-preset-expo": "^6.0.0",
    typescript: "^3.4.5"
  }
}

Please let me know what's wrong.


Metro bundler errors when using module-resolver

$
0
0

I've created a project using expo typescript template. Running on iOS and Android. No web.

I then set up path alias in tsconfig.json as follows:

"paths": {
  "@models/*": ["./src/models/*"],
  "@navigation/*": ["./src/navigation/*"],
  "@services/*": ["./src/services/*"],
  "@components/*": ["./tsx/components/*"],
  "@screens/*": ["./tsx/screens/*"],
  "@assets/*": ["./assets/*"]
}

Correspondingly, I configured babel.config.js as follows:

plugins: [
        [
            'module-resolver',
            {
                root: ['./'],
                alias: {
                    '@models': path.resolve(path.dirname('.'), 'src/models'),
                    '@navigation': path.resolve(path.dirname('.'), 'src/navigation'),
                    '@services': path.resolve(path.dirname('.'), 'src/services'),
                    '@screens': path.resolve(path.dirname('.'), 'tsx/screens'),
                    '@components': path.resolve(path.dirname('.'), 'tsx/components'),
                    '@assets': path.resolve(path.dirname('.'), 'assets'),
                }
            }
        ]
    ]

The above configuration works. App is bundled and runs fine. However the following non-critical errors are emitted during bundle:

transform[stderr]: Could not resolve "/Users/jblues/mobbiz/LOSMobileApp/src/navigation/AppNavigator" in file /Users/jblues/LOSMobileApp/tsx/App.tsx.
transform[stderr]: Could not resolve "/Users/jblues/LOSMobileApp/tsx/components/BottomTabNavigator" in file /Users/jblues/LOSMobileApp/src/navigation/AppNavigator.ts.
transform[stderr]: Could not resolve "/Users/jblues/mobbiz/LOSMobileApp/tsx/screens/Login" in file /Users/jblues/LOSMobileApp/src/navigation/AppNavigator.ts.

. . and so on. Is there something that I can add to my config to prevent these errors?

How to define navigation proptype in React navigation 5x

$
0
0

I am working new project. I started using typescript in this project. When I started the new project, I used react navigation 5x version. My question, how to define interface destructed navigation prop type in react navigation 5x version ? I don't use any type. I readed document react navigation, but i don't find my question. Thanks a lot. Have a nice days. 👋

import React from 'react';
import {Button, Text, SafeAreaView} from 'react-native';
import {inject, observer} from 'mobx-react';
import {Col, Grid} from 'react-native-easy-grid';
import Stores from '../../stores/storeIdentifier';
import ThemeStore from '../../stores/themeStore';

interface Props {
  navigation: any;
}
interface InjectedProps extends Props {
  themeStore: ThemeStore;
}

export function ModalComponent(props: Props) {
  const {themeStore, navigation} = props as InjectedProps;
  return (
    <SafeAreaView style={themeStore.styleSheet.globalModal__container}>
      <Grid>
        <Col
          style={[
            themeStore.styleSheet.alignCenter,
            themeStore.styleSheet.justifyCenter,
          ]}>
          <Text>Welcome to Global Modal 👋</Text>
          <Button onPress={() => navigation.goBack()} title="Back" />
        </Col>
      </Grid>
    </SafeAreaView>
  );
}
export const Modal = inject(Stores.ThemeStore)(observer(ModalComponent));

jest test freezes with typescript

$
0
0

Repo with reproduction: https://github.com/nmatushevskiy/jest-with-typescript

Hello everyone, i've had a strange issue.

I'm using jest, typescript, react-native, expo, babel, and components library - native base.

I needed to override some styles of native base components, found a guide and executed the command: node node_modules/native-base/ejectTheme.js from guide.
Long story short. A folder "native-base-theme" has been created that contains the styles of the components and combines them in a file index.js

Because project on typescript, i renamed all created files from js to ts.
Then, in a file i overridden styles for button component as recommended in the guide and tried to test it.
file with test: file

After npm run test, executable test freezes. As shown in the picture: console screenshot

I tried to mock methods from native-base-theme/components. It doesn't help.
Also tried to debug it with debugger (in index.test.ts shows where the debuggers were placed), but none of the debuggers been called.
Then i tried to solve problem by babel.config and jest.config, using suggested solutions of similar problems. It doesn't help too.

However, when i revert native-base-theme files from ts to js, test successfully completing.
googling doesn't help.
Also, tests works with some simple exported typescript functions.

For example:
some-file.ts

export function sum(a: number, b: number): number {
    return a + b;
}

Please, can anyone explain the reasoning of this behavior and how to fix it.
Thank you in advance.

react-native-svg ForeignObject element not found (React-Native + Expo + TypeScript)

$
0
0

I have a React Native project created with Expo. I also used the expo TypeScript configuration.

I installed react-native-svg using "expo install." I currently have version 9.13.3 installed.

Whenever I try to render the SVG using react-native-svg I get this error.

I've looked through the src files of the node module and can't find any files for the ForeignObject element.


Error:

Unable to resolve module `./elements/ForeignObject` from `node_modules/react-native-svg/src/ReactNativeSVG.ts`: 

None of these files exist:
  * node_modules/react-native-svg/src/elements/ForeignObject(.native|.ios.expo.ts|.native.expo.ts|.expo.ts|.ios.expo.tsx|.native.expo.tsx|.expo.tsx|.ios.expo.js|.native.expo.js|.expo.js|.ios.expo.jsx|.native.expo.jsx|.expo.jsx|.ios.ts|.native.ts|.ts|.ios.tsx|.native.tsx|.tsx|.ios.js|.native.js|.js|.ios.jsx|.native.jsx|.jsx|.ios.json|.native.json|.json|.ios.wasm|.native.wasm|.wasm)
  * node_modules/react-native-svg/src/elements/ForeignObject/index(.native|.ios.expo.ts|.native.expo.ts|.expo.ts|.ios.expo.tsx|.native.expo.tsx|.expo.tsx|.ios.expo.js|.native.expo.js|.expo.js|.ios.expo.jsx|.native.expo.jsx|.expo.jsx|.ios.ts|.native.ts|.ts|.ios.tsx|.native.tsx|.tsx|.ios.js|.native.js|.js|.ios.jsx|.native.jsx|.jsx|.ios.json|.native.json|.json|.ios.wasm|.native.wasm|.wasm)

ABI36_0_0facebook::ABI36_0_0React::JSIExecutor::defaultTimeoutInvoker(std::__1::function<void ()> const&, std::__1::function<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > ()>)
ABI36_0_0facebook::ABI36_0_0React::JSIExecutor::defaultTimeoutInvoker(std::__1::function<void ()> const&, std::__1::function<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > ()>)
B9D95EAB-9269-367D-B2F4-C2B45821A32D
B9D95EAB-9269-367D-B2F4-C2B45821A32D
B9D95EAB-9269-367D-B2F4-C2B45821A32D
7519E999-1053-3367-B9D5-8844F6D3BDC6
7519E999-1053-3367-B9D5-8844F6D3BDC6
CFRunLoopRunSpecific
GSEventRunModal
UIApplicationMain
Exponent
7B531A15-3E73-3185-90E2-B88D9476DA5E

Here is my component declaration - Star.tsx:

import * as React from "react";
import { Svg, Defs, Path } from "react-native-svg";

class StarIcon extends React.Component<{}, {}> {

  render() {
    return (
      <Svg width={31.709} height={31.814} viewBox="0 0 31.709 31.814" fill="black">
        <Defs />
        <Path
          d="M15.5,23.7,23.843,29l-2.214-9.99L29,12.288l-9.707-.867L15.5,2l-3.793,9.422L2,12.288,9.371,19.01,7.157,29Z"
          transform="translate(0.355 0.677)"
        />
      </Svg>
    )
  }
};

export default StarIcon;

I'm not sure what the issue is, other than it might have something to do with the TypeScript config. I created the same icon in a JavaScript react-native project and it worked fine.

If anyone knows why I'm getting this error or could provide any help I'd greatly appreciate it!

Thanks.

createStackNavigator and createBottomTabNavigatorin React Navigation v.5

$
0
0

I am new to react native, and i'm using new react-navigation v.5 in my react-native app. When i use createStackNavigator and createBottomTabNavigator together in NavigationContainer i have errors - "undefined is not an object" and "Another navigator is already registered for this container. You likely have multiple navigators under a single "NavigationContainer" or "Screen". Make sure each navigator is under a separate "Screen" container." (in React Navigation v.5). Please tell me where i`m wrong?

AppNavigation.tsx

import React from "react";
import { NavigationContainer } from "@react-navigation/native";
import { createStackNavigator } from "@react-navigation/stack";
import { MainScreen } from "../screens/MainScreen";
import { PostScreen } from "../screens/PostScreen";
import { AboutScreen } from "../screens/AboutScreen";
import { BookedScreen } from "../screens/BookedScreen";
import { CreateScreen } from "../screens/CreateScreen";
import { THEME } from "../theme";
import { Platform } from "react-native";
import { AppHeaderIcon } from "../components/AppHeaderIcon";
import { HeaderButtons, Item } from "react-navigation-header-buttons";
import { createBottomTabNavigator } from "@react-navigation/bottom-tabs";

type RootStackParamList = {
  Main: undefined;
  Post: { postId?: string; date?: string; booked?: boolean };
  About: undefined;
  Booked: undefined;
  Create: undefined;
};

const headerButtons = (title: string, icon: string, callback: () => void) => {
  return (
    <HeaderButtons HeaderButtonComponent={AppHeaderIcon}>
      <Item title={title} iconName={icon} onPress={() => callback()} />
    </HeaderButtons>
  );
};

export const AppNavigation = () => {
  const Stack = createStackNavigator<RootStackParamList>();
  const Tab = createBottomTabNavigator();

  return (
    <NavigationContainer>
      <Stack.Navigator
        initialRouteName="Main"
        screenOptions={{
          headerTintColor:
            Platform.OS === "android" ? "white" : THEME.MAIN_COLOR,
          headerStyle: {
            backgroundColor:
              Platform.OS === "android" ? THEME.MAIN_COLOR : "white"
          }
        }}
      >
        <Stack.Screen
          name="Main"
          component={MainScreen}
          options={{
            headerTitle: "Мой блог",
            headerRight: () =>
              headerButtons("Сделать фото", "ios-camera", () =>
                console.log("Press camera")
              ),
            headerLeft: () =>
              headerButtons("drower", "ios-menu", () =>
                console.log("Press drower button")
              )
          }}
        />
        <Stack.Screen
          name="Post"
          component={PostScreen}
          options={({ route }) => ({
            headerTitle: `Пост от ${new Date(
              route.params.date
            ).toLocaleDateString()}`,
            headerRight: () =>
              headerButtons(
                "star",
                route.params.booked ? "ios-star" : "ios-star-outline",
                () => console.log("Press star button")
              )
          })}
        />
        <Stack.Screen name="About" component={AboutScreen} />
        <Stack.Screen name="Booked" component={BookedScreen} />
        <Stack.Screen name="Create" component={CreateScreen} />
      </Stack.Navigator>
      <Tab.Navigator>
        <Tab.Screen name="Post" component={PostScreen} />
        <Tab.Screen name="Booked" component={BookedScreen} />
      </Tab.Navigator>
    </NavigationContainer>
  );
};

App.tsx

import React, { useState } from "react";
import { AppLoading } from "expo";
import { bootstrap } from "./src/bootstrap";
import { AppNavigation } from "./src/navigation/AppNavigation";

export default function App() {
  const [isReady, setIsReady] = useState(false);

  if (!isReady) {
    //AppLoading - пока не завершится выполнение код дальше не пойдет
    return (
      <AppLoading
        startAsync={bootstrap}
        onFinish={() => setIsReady(true)}
        onError={err => console.log('AppLoading error - ', err)}
      />
    );
  }

  return <AppNavigation />;
}

The Keyboard cover TextInput form when I run the React Native Expo app

$
0
0

When I run the app with expo and use my DateFormInput component the Keyboard covers TextInput. I tried to use I , "@pietile-native-kit/keyboard-aware-scrollview", "@types/react-native-keyboard-spacer", "react-native-keyboard-aware-scroll-view" and "react-native-keyboard-spacer", but none of them worked on either IOS or Android.

Here is the component I where I render my TextInput (DateFormInput):

import React from 'react';
import {
  Text,
  StyleSheet,
  View,
  KeyboardAvoidingView,
  ScrollView,
} from 'react-native';

import Box from './Box';
import Button from './Button';
import DateFormInput from './DateFormInput';
import { isValidDate } from '../util';
import strings from '../strings';

interface Props {
  handleSubmit: (formName: string) => void;
  handleOnChange: (formName: string, input: string) => void;
  handleCancelButton: (formName: string) => void;
  toggleInputError: (formName: string, clear?: boolean) => void;
  inputFormText: string;
  inputError: boolean;
  formName: string;
}

export const DDCAndDTNInputForm: React.FC<Props> = (props: Props) => {
  return (
    <Box style={styles.box}>
      <Text>
        {props.formName === 'DrugTest' ? strings.DrugTestDate : strings.DDCDate}
      </Text>
      <View style={{ flex: 1 }}>
        <KeyboardAvoidingView behavior="padding" style={styles.box}>
          <ScrollView style={{ flex: 1 }}>
            <DateFormInput
              type={'datetime'}
              options={{ format: 'MM/DD/YYYY' }}
              placeholder={'MM/DD/YYYY'}
              value={props.inputFormText}
              error={props.inputError}
              onChangeText={text => props.handleOnChange(props.formName, text)}
              onBlur={e => {
                isValidDate(props.inputFormText)
                  ? props.handleSubmit(props.formName)
                  : props.toggleInputError(props.formName, true);
              }}
            />
          </ScrollView>
        </KeyboardAvoidingView>
      </View>

      <Text style={{ color: 'red' }}>
        {props.inputError ? 'Type a valid date' : null}
      </Text>

      <View
        style={{
          flexDirection: 'row',
          justifyContent: 'space-around',
        }}
      >
        <Button
          viewStyle={styles.linkButton}
          title={'CANCEL'}
          onPress={() => {
            props.handleCancelButton(props.formName);
          }}
        />

        <Button
          viewStyle={styles.linkButton}
          title={'SAVE DATE'}
          onPress={() => {
            isValidDate(props.inputFormText)
              ? props.handleSubmit(props.formName)
              : props.toggleInputError(props.formName, true);
          }}
        />
      </View>
    </Box>
  );
};

export default DDCAndDTNInputForm;

const styles = StyleSheet.create({
  linkButton: {
    paddingVertical: 8,
    paddingHorizontal: 16,
    marginVertical: 8,
    flex: 1,
  },
  box: {
    padding: 24,
    marginBottom: 16,
    flex: 1,
  },
});

This is my custom InputText (DateFormInput):

import React from 'react';
import {
  Text,
  StyleProp,
  TextStyle,
  StyleSheet,
  KeyboardType,
  View,
  NativeSyntheticEvent,
  TextInputFocusEventData,
} from 'react-native';
import { TextInputMask } from 'react-native-masked-text';

import { textStyle, colors } from '../styles';

const styles = StyleSheet.create({
  input: {
    marginVertical: 8,
    padding: 16,
    borderWidth: 2,
    borderColor: colors.sectionBackground,
    borderRadius: 8,
    backgroundColor: colors.white,
  },
});

export default (props: {
  label: string;
  value: string;
  labelStyle: StyleProp<TextStyle>;
  keyboardType?: KeyboardType;
  onChangeText: (text: string) => void;
  onBlur?: (e: NativeSyntheticEvent<TextInputFocusEventData>) => void;
  error?: boolean;
  defaultValue?: string;
}) => (
  <View style={{ flexDirection: 'column', alignContent: 'stretch' }}>
    <Text style={[textStyle.formLabel, props.labelStyle]}>{props.label}</Text>
    <TextInputMask
      type={'datetime'}
      options={{ format: 'MM/DD/YYYY' }}
      placeholder={'MM/DD/YYYY'}
      style={[styles.input, props.error ? { borderColor: colors.red } : null]}
      value={props.value}
      onChangeText={props.onChangeText}
      onBlur={props.onBlur}
    />
  </View>
);

using REGEX on textInput in react native

$
0
0

I can't seem to make my regex work so i think I must be doing something wrong. If anyone could help me out that would be great.

here is my regex function

let validatePlate = (plate) => {
  var re = /(^[A-Z]{2}[0-9]{2}\s?[A-Z]{3}$)|(^[A-Z][0-9]{1,3}[A-Z]{3}$)|(^[A-Z]{3}[0-9]{1,3}[A-Z]$)|(^[0-9]{1,4}[A-Z]{1,2}$)|(^[0-9]{1,3}[A-Z]{1,3}$)|(^[A-Z]{1,2}[0-9]{1,4}$)|(^[A-Z]{1,3}[0-9]{1,3}$)|(^[A-Z]{1,3}[0-9]{1,4}$)|(^[0-9]{3}[DX]{1}[0-9]{3}$)/

    return re.test(plate);
};

and I use it in textInput as follows:

<TextInput
onChange={validatePlate}
editable={true}
}}>
Hello
</TextInput>

It still allows special characters, etc.

FYI: The regex is for filtering UK numberplates.


TypeScript error Parameter 'props' implicitly has an 'any' type

$
0
0

I am testing out how to use Context in React but for some reason I cant run the app due the typescript error I get!

Code:

import React from 'react';
import './App.css';


class App extends React.Component {
    render() {
      return <Toolbar theme="dark" />;
    }
  }

  function Toolbar(props) {
    // The Toolbar component must take an extra "theme" prop
    // and pass it to the ThemedButton. This can become painful
    // if every single button in the app needs to know the theme
    // because it would have to be passed through all components.
    return (
      <div>
        <ThemedButton theme={props.theme} />
      </div>
    );
  }

  class ThemedButton extends React.Component {
    render() {
      return <button className={'btn btn-' + this.props.theme}></button>;
    }
  }

export default App;

Error I get:

C:/Users/as/Desktop/React - Mobx -Hooks/react-hooks-mobx/src/App.tsx
TypeScript error in C:/Users/iluli/Desktop/React - Mobx -Hooks/react-hooks-mobx/src/App.tsx(11,20):
Parameter 'props' implicitly has an 'any' type.  TS7006

     9 |   }
    10 |   
  > 11 |   function Toolbar(props) {

Any idea how to fix this and explain the reason why it throws that error?

Why does my React Native app build successfully despite TypeScript compiler error?

$
0
0

I've recently started using TypeScript with Expo. I've done all the linter/formatter integrations like typescript-eslint so I can catch most of the errors during coding. To check if the code compiles, I run npx tsc every once in a while and fix accordingly.

One thing that I haven't fully grasped yet is why my app builds successfully even when there are numerous compile errors. I expect (and prefer) to see a red screen error for every compile error rather than the app build successfully and me find it out later. For example,

function square<T>(x: T): T {
  console.log(x.length); // error TS2339: Property 'length' does not exist on type 'T'.
  return x * x;
}

is a typical TypeScript error that (I believe?) can be easily checked at compile time. I want it to result in a big red screen error and the build to fail.

I'm quite new to TypeScript so it's possible that I'm missing something very important. What exactly is causing this leniency and is there a way to enforce stricter checks?

Sublime text cannot find imported node modules

$
0
0

Sublime Text 3 underlines all of my imports, giving me the error - any ideas?

Cannot find module....

enter image description here

tsconfig:

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "jsx": "react-native",
    "lib": ["es6"],
    "module": "es6",
    "moduleResolution": "node",
    "noEmit": true,
    "noImplicitAny": true,
    "target": "es6"
  },
  "jsRules": {"no-empty": true},
  "exclude": ["node_modules"]
}

React Native High Order Component for State Component

$
0
0

I'm trying to do a hoc for a state component in React Native with Typescript which is defined below:

export function hoc<T extends ComponentClass>(target: any): T {
  return (target: any) =>
    class MyClass extends Component<any, any> {

    }
}

But I get the error Type '(target: any) => typeof MyClass' is not assignable to type 'T'

What should I change to make it correct with typescript?

How to use react-native-webview-leaflet?

$
0
0

I'm devolopping an app using React Native and Expo. And I want to integrate react-native-webview-leaflet. But when I want to use the tag WebViewLeaflet so that I can be able to use WMS with GeoServer, it underlines the tag then shows this error when I hover the cursor.

JSX element type 'WebViewLeaflet' does not have any construct or call signatures.ts(2604)

Peek Problem No quick fixes available.

And when I try to run I get this errors

Some times this one

Uncaught Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in The fact, I have followed the step of installation but nothing work fine for me.

and other time this one

WebView has been removed from React Native. It can now be installed and imported from 'react-native-webview' instead of 'react-native'. See https://github.com/react-native-community/react-native-webview

Here is my code

import React, {Component} from 'react';
import WebViewLeaflet from 'react-native-webview-leaflet';
import { View } from 'react-native';

 export default class Map extends Component{
    render(){
      return (
        <View>
            <WebViewLeaflet/>
        </View>
      )
    }
}

Package.json

{
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject"
  },
  "dependencies": {
    "@react-native-community/masked-view": "^0.1.6",
    "expo": "~36.0.0",
    "expo-asset": "^8.0.0",
    "expo-file-system": "^8.0.0",
    "expo-font": "^8.0.0",
    "leaflet": "^1.3.4",
    "react": "~16.9.0",
    "react-dom": "~16.9.0",
    "react-native": "https://github.com/expo/react-native/archive/sdk-36.0.0.tar.gz",
    "react-native-dynamic-vector-icons": "^0.2.1",
    "react-native-gesture-handler": "^1.5.3",
    "react-native-maps": "^0.26.1",
    "react-native-paper": "^2.2.2",
    "react-native-reanimated": "^1.5.0",
    "react-native-safe-area-context": "^0.6.2",
    "react-native-screens": "^2.0.0-alpha.12",
    "react-native-vector-icons": "^6.6.0",
    "react-native-web": "~0.11.7",
    "react-native-webview-leaflet": "^4.3.4",
    "react-navigation": "^4.0.10",
    "react-navigation-drawer": "^2.3.3",
    "react-navigation-material-bottom-tabs": "^2.1.5",
    "react-navigation-stack": "^2.0.15",
    "react-navigation-tabs": "^2.7.0"
  },
  "devDependencies": {
    "@types/react": "~16.9.0",
    "@types/react-native": "~0.60.23",
    "@babel/core": "^7.0.0",
    "babel-preset-expo": "~8.0.0",
    "typescript": "~3.6.3"
  },
  "private": true
}

Double tab bar navigation to move together

$
0
0

i want to have tab bar navigation and under it another tab bar navigation and they can move together, as in the image below lets say we have screen 1,2,3 in a tab bar navigation , standing on screen 1 slide up to navigate to screen 4 then slide right (with tab bar sweep animation interpolate) to get screen 5

enter image description here

Action not dispatching using react/ redux/ typescript

$
0
0

I get the error:

node_modules\react-native\Libraries\Core\ExceptionsManager.js:86 TypeError: Cannot read property 'videos' of undefined

This error is located at: in Connect(HomeHeader) (at AppNavigator.tsx:116) in RCTView (at View.js:35)

App.tsx:

const App = () => {
  return (
    <ImageBackground
      source={require('./assets/images/TC_background.jpg')}
      style={styles.container}>
      <Provider store={store}>
        <PersistGate loading={null} persistor={persistor}>
          <AppNavigator
            ref={navigatorRef => {
              NavigationService.setTopLevelNavigator(navigatorRef);
            }}
          />
        </PersistGate>
      </Provider>
    </ImageBackground>
  );
};

AppNavigator.tsx:

const config = {
    contentOptions: {
        activeTintColor: '#e91e63',
        inactiveTintColor: '#ffffff',
        itemStyle: {
            flexDirection: 'row-reverse',
        },
    },
    drawerWidth: 200,
    drawerPosition: 'left',
    contentComponent: SideMenu,
    drawerBackgroundColor: '#00000020',
    cardStyle: {
        backgroundColor: 'transparent',
        opacity: 1,
    },
};

const withHeader = (
    screen: Function,
    routeName: string,
    Header: any,
): StackNavigator =>
    createStackNavigator(
        {
            [routeName]: {
                screen,
                navigationOptions: ({routeName, props}) => ({
                    header: props => <Header {...props} />,
                }),
            },
        },
        {
            initialRoute: 'Home',
            cardStyle: {
                backgroundColor: 'transparent',
                opacity: 1,
            },
            transitionConfig: () => ({
                containerStyle: {
                    backgroundColor: 'transparent',
                },
            }),
        },
    );

const routes = {
    ....
};

const NestedDrawer = createDrawerNavigator(routes, config);

const MainStack = createStackNavigator(
    {
        Home: {
            screen: HomeScreen,
            navigationOptions: ({ props }) => ({
                header: (props: any) => <HomeHeader {...props} />,
            }),
        },
        ...
    },
    {
        initialRoute: 'Home',
        cardStyle: {
            backgroundColor: 'transparent',
            opacity: 1,
        },
    },
);

export default createAppContainer(MainStack);

HomeHeader:

class HomeHeader extends Component {
    constructor(props) {
        super(props);
        this.state = { term: "", videos: [] };
        this.onSubmitEdit = this.onSubmitEdit.bind(this);
    }
    componentDidMount() {
        this.SearchFilterFunction("");
    }
    SearchFilterFunction(term) {
        const { filteredVideo } = this.props;
        filteredVideo(term);
    }

    onSubmitEdit() {
        const {
            navigation: { navigate },
            initSearch
        } = this.props;

        navigate("VideoEpisodes");
    }

    onPress(id) {
        const {
            navigation,
            initSearch,
            navigation: { dispatch }
        } = this.props;
        initSearch();

        const resetAction = StackActions.reset({
            index: 0,
            actions: [
                NavigationActions.navigate({
                    routeName: "VideoPlayer",
                    params: { id }
                })
            ]
        });
        dispatch(resetAction);
    }
    separator = () => <View style={styles.separator} />;
    render() {
        const {
            navigation,
            videos,
            search: { term },
            scene: {
                route: { routeName: title }
            }
        } = this.props;
        return (
            <..... />
        );
    }
}
const mapDispatchToProps = dispatch => ({
    filteredVideo: data => dispatch(filteredVideo(data)),
    initSearch: () => dispatch(initSearch())
});

const mapStateToProps = state => {
    return {
        videos: state.iaApp.videos,
        search: state.iaApp.search
    };
};

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(HomeHeader);

const styles = StyleSheet.create({
    container: {
        flexDirection: "row",
        justifyContent: "space-between",
        alignItems: "center",
        padding: 10,
        marginTop: Platform.OS === "ios" ? 20 : 0,
        paddingTop: Platform.OS === "ios" ? 20 : 10
    },
    title: {
        color: "#fff",
        fontSize: 18
    },
    separator: {
        borderBottomColor: "#d1d0d4",
        borderBottomWidth: 1
    }
});

HomeScreen:

class HomeScreen extends React.Component<any, any> {
  constructor(props: any) {
    super(props);
  }
  componentDidMount() {
    const {fetchData} = this.props;
    fetchData();
  }

  render() {
    const {videos} = this.props;

    return (
      <View style={styles.container}>
        <HomeMenu {...this.props} />
      </View>
    );
  }
}

const mapDispatchToProps = (dispatch: any) => ({
  fetchData: () => dispatch(fetchData()),
});
const mapStateToProps = (state: any) => {
  return {
    videos: state.IaApp.videos,
  };
};
export default connect(
  mapStateToProps,
  mapDispatchToProps,
)(HomeScreen);

TypeScript Property 'navigation' is missing in type but required in type 'Props' React Native

$
0
0

In a React Native App, I understand that when you pass your RootNavigator inside createAppContainer and export it, the navigation prop is implicitly made available in all child components.

export default createAppContainer(AppNavigator);

I have this component that is supposed to navigate to another screen using the navigation prop. I defined an interface for the props of this component like this:

props interface for ComponentA:

interface Props {
  count: number;
  navigation: NavigationStackProp<{quizId: number}>;
}

Now I render ComponentA like this:

...
render() {
    return (
        <ComponentA count={0} /> // TypeScript screams here that I am not passing the navigation props
    );
}

If I pass the navigation prop with ComponentA like this:

<ComponentA count={0} navigation={props.navigation} />

TypeScript is satisfied.

My question therefore is, do I have to pass navigation prop explicitly like this? It doesn't feel right to me. Is there a way I can suppress TypeScript warnings for this particular situation.

I am new to TypeScript so this might be something trivial but I will appreciate any help.

Thanks

Issues With Arrow Syntax and Binding in React for Grandchild Element

$
0
0

I'm trying to create a test page and I'm having trouble with getting some buttons to work.

import React from 'react'
import { Dropdown, Icon, Menu, Segment } from 'semantic-ui-react'

export class Page extends React.Component<any, any> {

    constructor(props: any) {
        super(props);
        this.state = {
            sidebarVisible: false
        }
    }

    render() {
        return (
            <Header name='test' onCLick={this.handleIconClick}/>
        )
    }

    handleIconClick = () => {

        console.log('CLICKED');
        let visible = !this.state.sidebarVisible;
        this.setState({sidebarVisible: visible});

    }

}

export class Header extends React.Component<any, any> {

    constructor(props: any) {
        super(props);
        this.state = {
            iconName: this.props.visible ? 'angle double left' : 'angle double right'
        };
        console.log(this.props)
    }

    render() {
        return (
            <div>
                <Menu>
                    <Menu.Item onClick={this.props.onClick}>
                        <Icon name={this.state.iconName}/>
                    </Menu.Item>
                </Menu>
            </div>
        )
    }
}

In the above example which I've arrived at from following documentation, looking online for solutions, etc... does not even allow me to click the button ("CLICKED" is never logged).

When I change

<Menu.Item onClick={this.props.onClick}>

to

<Menu.Item onClick={() => this.props.onClick()}>

I get an error stating that:

this.props.onClick is not a function

I've spent quite some time on this so your help would be greatly appreciated.

Thanks.

JSX element type 'HTMLImageElement' is not a constructor function for JSX elements

$
0
0
I am trying an example with React Native and Typescript and for the following code: 

<Image
        style={{
          width: 51,
          height: 51,
          resizeMode: 'contain',
        }}
        source={{ uri: portretPhoto }}
        style={styles.uploadedImage}
        resizeMode="contain"
      />

I get error: JSX element type 'HTMLImageElement' is not a constructor function for JSX elements. Type 'HTMLImageElement' is missing the following properties from type 'ElementClass': render, context, setState, forceUpdate, and 3 more.

How do I fix it?

Sentry: beforeSend is called mutliple times

$
0
0

I have a react-native app with redux. I want to fetch a PUT request with data when Sentry logs an error. I’m new in Sentry and I use this in my App.js:

Sentry.init({
  dsn: ...,
  enableInExpoDevelopment: true,
  debug: true,

  beforeSend(event) {
try {
  fetchMyData(data)
  Sentry.withScope(function(scope) {
    Sentry.captureMessage('Error');
  });
  return event;
}
catch (e) { 
  return e;
}
  },
});

It works and the request is successfull the most time, but beforeSend is called mutliple times and in Sentry the events loop: Put Request -> captureMessage -> Put Request -> captureMessage… and so on.

Is there another way for calling the method when Sentry logs? The data is only defined at the moment when beforeSend is called. why is there this loop?

Thank you!

How to execute a function when some item renders in react native?

$
0
0

I have a sectionlist of Contacts where I am displaying both device and online contacts of a user. The online contacts api doesnt give me all the contacts at once. So I have to implement some pagination. I am also fetching all device contacts and first page of online contacts and sorting them to show in sectionlist, but the problem is, to load more contacts, i have to keep track of the last item rendered in my state and in the render function I am calling pagination function to load more contacts. and then i am updating the state of fetched online contact. But its an unsafe operation, is there a better way to achieve this?

I want to execute a function when the specific item renders and it can update the state.

Here is some code: ContactList.tsx

import React, { Component } from "react";
import {
    View,
    StyleSheet,
    SectionListData,
    SectionList,
    Text
} from "react-native";

import { Contact } from "../../models/contact";
import ContactItem from "./contact-item";

export interface ContactsProps {
    onlineContacts: Contact[];
    deviceContacts: Contact[];
    fetchMoreITPContacts: () => void;
}

export interface ContactsState {
    loading: boolean;
    error: Error | null;
    data: SectionListData<Contact>[];
    lastItem: Contact;
    selectedItems: [];
    selectableList: boolean;
}

class ContactList extends Component<ContactsProps, ContactsState> {
    private sectionNames = [];

    constructor(props: ContactsProps, state: ContactsState) {
        super(props, state);
        this.state = {
            loading: false,
            error: null,
            data: [],
            lastItem: this.props.onlineContacts[this.props.onlineContacts.length - 1]
        };
        for (var i = 65; i < 91; ++i) {
            this.sectionNames.push({
                title: String.fromCharCode(i),
                data: []
            });
        }
    }

    private buildSectionData = contacts => {
        this.sort(contacts);
        const data = [];
        const contactData = this.sectionNames;
        contacts.map(contact => {
            const index = contact.name.charAt(0).toUpperCase();
            if (!data[index]) {
                data[index] = [];
                contactData.push({
                    title: index,
                    data: []
                })
            }
            data[index].push(contact);
        });
        for (const index in data) {
            const idx = contactData.findIndex(x => x.title === index);
            contactData[idx].data.push(...data[index]);
        }
        this.setState({
            loading: false,
            error: null,
            lastItem: contacts[contacts.length - 1],
            data: [...contactData]
        });
    };

    private sort(contacts) {
        contacts.sort((a, b) => {
            if (a.name > b.name) {
                return 1;
            }
            if (b.name > a.name) {
                return -1;
            }
            return 0;
        });
    }

    componentDidMount() {
        const contacts = [].concat(
            this.props.deviceContacts,
            this.props.onlineContacts
        );
        this.buildSectionData(contacts);
    }

    componentDidUpdate(
        prevProps: Readonly<ContactsProps>,
        prevState: Readonly<ContactsState>,
        snapshot?: any
    ): void {
        if (this.props.onlineContacts !== prevProps.onlineContacts) {
            const from = this.props.itpContacts.slice(
                prevProps.onlineContacts.length,
                this.props.onlineContacts.length
            );
            this.buildSectionData(from);
        }
    }

    renderItem(item: any) {
        if (!!this.state.lastItem && !this.state.loading)
            if (item.item.id === this.state.lastItem.id) {
                this.setState({
                    loading: true
                });
                this.props.fetchMoreOnlineContacts();
            }
        return <ContactItem item={item.item} />;
    }

    render() {
        return (
            <View style={styles.container}>
                <SectionList
                    sections={this.state.data}
                    keyExtractor={(item, index) => item.id}
                    renderItem={this.renderItem.bind(this)}
                    renderSectionHeader={({ section }) =>
                        section.data.length > 0 ? (
                            <Text style={styles.sectionTitle}>
                                {section.title}
                            </Text>
                        ) : null
                    }
                />
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1
    },
    sectionTitle: {
        paddingBottom: 30,

        paddingLeft: 25,
        fontWeight: "bold",
        fontSize: 20
    }
});

export default ContactList;

Viewing all 6413 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>