Intergrating Redux into a React- Native app that uses react-navigation - react-native

I'm in the process of developing a mobile application that will need to manage the state of the entire application. To solve this issue I decided to integrate redux into the Tech stack. When I attempt to connect my redux store to the application to access information contained in the store I get an error in my simulator that tells me I need to pass the Provider with a state property as a parent to the App component.
I attempt to do this through my index.js file on the top level of my file system; Within the AppRegistry registerComponent method, wrapping the "appName" with the Provider with the store added as a prop, with no luck.
Any suggestions will be helpful.
https://github.com/Wheaties247/filcrum

Doesn't seem like you have setup redux in your project.
You should do something like this on your main component
import React from 'react';
import { AppRegistry } from 'react-native';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import AppReducer from './src/reducers';
import { AppNavigator, middleware } from './src/navigators/AppNavigator';
const store = createStore(AppReducer, applyMiddleware(middleware));
class ReduxExampleApp extends React.Component {
render() {
return (
<Provider store={store}>
<AppNavigator />
</Provider>
);
}
}
AppRegistry.registerComponent('ReduxExample', () => ReduxExampleApp);
export default ReduxExampleApp;
check react navigation docs for more information about redux integration
https://reactnavigation.org/docs/en/redux-integration.html#docsNav

Related

React Native Hook problem [ useNavigation ]

I am writing applications with React Native. I am using typescript. I am using Hook and getting an error in the application. When I searched, Hook is valid as of React-Native version 0.59.0 but I'm having trouble.
How can I solve it?
Hook Issue App
http://prnt.sc/vvkotk
import { useNavigation } from "#react-navigation/native";
import React from "react";
import { Dimensions, Image, StyleSheet } from "react-native";
import { Box, Header, Text } from "../../components";
import { useTheme } from "../../components/Theme";
const Drawer = () => {
const navigation = useNavigation();
const theme = useTheme();
return (
<Box flex={1}>
<Box flex={0.2} backgroundColor="white">
<Box
position="absolute"
top={0}
left={0}
right={0}
bottom={0}
borderBottomRightRadius="xl"
backgroundColor="secondary"
>
It looks like it doesn't recognise your custom useTheme hook. Please make sure the filename starts with use so useTheme.tsx of useTheme.jsx. Check the filename of your Drawer component as well to be sure. It should contain .jsx or .tsx.

Is there any example of using StrictMode with React-Native?

As I know about it, This checks and gives warnings for React-Native code and its lifecycles.
I read about it from What is StrictMode in react?
How can I use it in react native ?
Here is a simple example to use StrictMode in React Native
StrictMode can be directly imported from React and can used like wrapping up View inside it.
import React, {Component, StrictMode} from 'react';
import {View} from 'react-native';
class Example extends Component {
render() {
return (
<StrictMode>
<View />
</StrictMode>
);
}
}
export default Example;

Should Redux Devtools Extension be installed as a dev dependancy when using composeWithDevTools()?

I decided to add the RNDebugger and Redux Devtools Extension packages to my app for help debugging and for later testing with redux persist (which I haven't implemented yet). Thus, I am now using the the following packages for my React Native app's state architecture: React Redux, Redux, Redux Thunk, RNDebugger, and Redux Devtools Extension. The RNDebugger and Redux Devtools Extension was installed as a dev dependency, per their docs. I am using the composeWithDevTools function when creating my redux store. So I am wondering, do I have to install the RNDebugger and Redux Devtools Extension as production dependencies instead since the composeWithDevTools function is part of the Redux Devtools Extension package? I do not wish to use the debugger in Production, but I also don't want the app to fail when it goes to Production because the composeWithDevTools function is unknown. My code is working correctly and I can see the redux store in the RNDebugger (nice!). Nonetheless, this my basic setup:
// Store.js
import { createStore, applyMiddleware } from 'redux';
import { composeWithDevTools } from "redux-devtools-extension";
import ReduxThunk from 'redux-thunk';
import reducers from '#reducers/';
export default createStore(
reducers,
composeWithDevTools(
applyMiddleware(ReduxThunk)
)
);
// App.js
import React, { Component } from 'react';
import { Provider } from 'react-redux';
import Store from './Store';
import Router from './Router';
class App extends Component {
render() {
return (
<Provider store={Store}>
<Router />
</Provider>
);
}
}
export default App;

React-native QR code scanner is throwing error

I have a react-native app where I have developed a scanner feature using react-native-qrcode-scanner.However, when I try to scan the data, I get the below error-
error: can't find variable navigation
I see this error in onSuccess method at line authorizationToken.
My code-
import React, { Component } from 'react';
import {
Text,
View,
Image,
TouchableOpacity,
Linking
} from 'react-native';
import styles from '../assets/style';
import QRCodeScanner from 'react-native-qrcode-scanner';
export default class ScanScreen extends Component {
onSuccess(scanEvent) {
this.props.navigation.navigate("Result", {
'accessKey': scanEvent.data,
'authorizationToken':navigation.getParam('authorizationToken', undefined),
"userData": navigation.getParam('userData', undefined),
"methodName": "fetchData"
});
}
render() {
return (
<View style={styles.container}>
<QRCodeScanner
onRead={this.onSuccess.bind(this)}
/>
</View>
);
}
}
Any idea what I m missing here. Any help is much appreciated.Thanks in advance.
Make sure that Your Screen is registered in react-navigation config (follow this guide: can't find variable navigation).
Or pass navigation prop to it with HOC withNavigation: https://reactnavigation.org/docs/en/with-navigation.html. Instead export default class ScanScreen extends Component do class ScanScreen extends Component and at end of file do
export default withNavigation(ScanScreen);
Don't forget about importing Higher Order Component: import { withNavigation } from 'react-navigation';
Also be sure that all native parts are properly linked. For example react-native-gesture-handle (https://kmagiera.github.io/react-native-gesture-handler/docs/getting-started.html#linking).
navigation has to be part of props so accessing navigation using this.props.navigation solves this issue.

Trouble connecting react-native with redux

I am trying to create a counter with react-native and redux, but i'm getting the error Expected a component class, got [object Object].
This is my index.android.js
import React, { Component } from 'react';
import { AppRegistry } from 'react-native';
import Root from './src/containers/Root';
import configureStore from './configureStore';
export default class CounterReactNativeRedux extends Component {
render() {
return (<Root store={configureStore()} />)
}
}
AppRegistry.registerComponent('CounterReactNativeRedux', () => CounterReactNativeRedux);
My code can be found here.
Found answers about this saying that it might be because of the class name not being capitalize but this is not the case.
Anyone any idea?
Look inside of your Counter.js. You have used div tags there which do not exist in React Native.
So import View tag from React Native and use that.
import {View} from 'react-native'
const Counter = ({ value }) => (
<View>{value}</View>
);