Connecting the components with React-Redux - react-native

I'm using Expo React Native to develop a mobile application with the help of Redux, as well as React-Redux. With the React-Redux v6.0, I can connect the components with no issues, but I cannot get the state from the store with v6.0, although with Logger Middleware, my state is already updated. I cannot use it after extract it with mapStateToProps. So I decided to update React-Redux to v7.0 with the hope that the issues will be solved but I met a new one (Image below).
Issue Screen
This is my App.js
import React from 'react';
import { Platform, StyleSheet, Text, View} from 'react-native';
import {Provider} from 'react-redux';
import store from './src/redux/configureStore';
import LocationShow from './src/components/LocationShow';
export default class App extends React.Component {
render() {
return (
<Provider store={store}>
<View style={styles.container}>
<LocationShow />
</View>
</Provider>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
This is my LocationShow.js
import React from 'react';
import {View, Text, StyleSheet, ActivityIndicator} from 'react-native';
import {Location, Permissions, Constants} from 'expo';
import {connect} from 'react-redux';
import {Platform} from 'expo-core';
import * as locationActions from '../redux/actions/locationActions';
class LocationShow extends React.Component {
componentDidMount = async () =>{
if (Platform.OS === 'android' && !Constants.isDevice){
console.log('Switch to the real device dawg!')
}else{
await this.props.getCurrentLocation();
}
}
render(){
return(
<View>
{this.props.isLoading ?
<ActivityIndicator/>:
<View>
<Text>Latitude:{this.props.latitude}</Text>
<Text>Longitude:{this.props.longitude}</Text>
</View>
}
</View>
)
}
}
const mapStateToProps = (state) =>{
return{
latitude: state.latitude,
longitude: state.longitude,
error: state.error,
isLoading: state.isLoading
}
}
const mapDispatchToProps = (dispatch) =>{
return {
getCurrentLocation: () =>
dispatch(locationActions.getCurrentLocation())
}
}
export default connect(mapStateToProps, mapDispatchToProps)(LocationShow);
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center'
}
});
I read the changes of v7.0 with the connect() but Im quite new to React so I cannot get the idea of it.
Note: connect now uses React.memo() internally, which returns a special object rather than a function. Any code that assumed React components are only functions is wrong, and has been wrong since the release of React 16.6. If you were using PropTypes to check for valid component types, you should change from PropTypes.func to PropTypes.elementType instead.
Im running React:v16.8.6, React-Native:0.57.1, React-Redux: v7.0.2, Redux:v4.0.1
Hope someone can help me out, sorry if the code is too bad, as I said above, I'm quite new to React Native and Redux or other stuff.
Thank you!

After considering, I decied to go back to version 6.0.1 of react-redux. Now everything works like a champ. This thread can be closed right now.
By the way, the problem exactly comes from the new connect(), cause the react-redux team uses React.memo() and Hooks. But I still do not understand why it is the problem so if anyone has an idea about the changes of version 7.0.2 with the new connect(), feel free to explain.
Thank you guys!

Related

react native app crashes when tf.ready called

I followed all instructions to install #tensorflow/tfjs-react-native given at
https://www.npmjs.com/package/#tensorflow/tfjs-react-native/v/0.3.0
this is my app.js file:
import React, { useState, useEffect } from 'react';
import * as tf from '#tensorflow/tfjs';
import '#tensorflow/tfjs-react-native';
import {
SafeAreaView,
StatusBar,
StyleSheet,
Text,
View,
} from 'react-native';
export default () => {
const [ready, setReady] = useState(false)
useEffect(() => {
const load = async () => {
await tf.ready()
setReady(true)
}
load()
})
return (
<SafeAreaView style={{ backgroundColor: '#fff', flex: 1 }}>
<StatusBar barStyle={'dark-content'} />
<View>
<Text>hello</Text>
</View>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
});
App crashes when tf.ready() is called. no error is logged in console.
If i comment tf.ready() everything works fine. am i doing something wrong?
This is my package.json file
image of package.json file
How do i test if this package is installed correctly?
any help from your side will be appreciated.
I had this issue when running my app with expo on an android device, what solved it for me was setting the backend with :
await tf.setBackend('cpu');
before
tf.ready();
It might solve your problem as well

Expo Client does not save changes

Im very new to react-native and expo.
I can't solve this problem myself.
My previous code was this.
import React, {Component} from 'react';
import { View, Text, StyleSheet, TouchableOpacity } from 'react-native';
import firebase from 'firebase';
export default class Home extends React.Component {
handleSignOut = () => {
firebase
.auth()
.signOut()
.then(result => alert('sign out success'))
.catch(error => console.error(error));
};
render() {
return (
<View style={styles.container}>
<Text>Main</Text>
<TouchableOpacity onPress={this.handleSignOut}>
<Text>Sign Out</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center'
}
});
Then I just changed the text 'Main' to 'Home' and saved.
After that expo client was reloaded but nothing changed :(
I would appreciate it if you could give me any advices!
Not sure which version of Expo you're using (expo --version), but I spun off a quick Expo project, copy pasted your code (minus the Firebase parts) and it works seamlessly.
Maybe try closing and reopening your emulator/simulator.

MobX observable not refreshing in React Native

I am trying to hot refresh observable data in a react native view. The initial value is being displayed perfectly but does not refresh when I call an action from the store to change it. It is, however, being changed (I have another view where I can see the change) just not on the screen that calls the action to update. Any ideas? I am using React Navigation. Not sure if that is somehow interfering.
Store.js
import { observable, action, decorate, computed } from 'mobx'
class BooksStore {
bookColor = 'green'
testAction = () => {
console.log('change store value')
this.bookColor = 'blue'
}
}
decorate(BooksStore, {
loading: observable,
testAction: action,
});
export default new BooksStore();
App.js
import React from 'react';
import Routes from './Routes';
import { Provider } from 'mobx-react';
import BooksStore from './src/Stores/BooksStore';
export default function App() {
return (
<Provider booksStore={BooksStore}>
<Routes /> //react-navigation stacks
</Provider>
);
}
RN View:
import React, { Component } from 'react';
import { Image, View, Button, Alert, ScrollView, Dimensions, StyleSheet, Text } from 'react-native';
import { inject, observer, Observer} from 'mobx-react'
class MobX1 extends Component {
render() {
console.log(this.props.booksStore.bookColor)
return (
<View style={styles.container}>
//this displays initial store val but no refresh when I call testAction()
<Observer>{() =>
<Text style={{ marginBottom: 48 }}>{this.props.booksStore.bookColor}</Text>
}</Observer>
<Button
title="Change to Blue"
onPress={() => this.props.booksStore.testAction()} />
</View>
);
}
}
export default inject('booksStore')(observer(MobX1));
//------------------------------ styles -------------------------------//
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
});
Thanks for any help
Anyone that comes across this... I forgot to decorate properly... durrrrr
decorate(BooksStore, {
bookColor: observable, <<<<<<<<<<<<<
testAction: action,
});

React-native: Is it possible to use pure Flux with React Native and not its implementations like Redux etc.?

I was trying to get familiarized with React Native by doing a very simple project using React Native. I wanted the code to be clean and following some architecture. I've been using Flux with React for some time now and thought I could do the same with React Native as well. If I am wrong here , kindly let me know why is it not possible?
Assuming that I was correct, let me present the actual problem that I am facing. I am following the CRNA tutorial and using Expo to build and test. To follow Flux architecture. This is what I had done.
Installed Flux with npm install flux
Created a dispatcher.js file with the following code.
import { Dispatcher } from 'flux';
export default new Dispatcher();
Created a 'sampleactiondispatcher.js' that does:
import Dispatcher from '../actiondispatchers/dispatcher';
import ActionType from '../actiondispatchers/actiontype';
class SampleActionDispatcher {
saveSomething(value) {
Dispatcher.dispatch({
actionType: ActionType.SAMPLE_ACTION,
payload: value
});
}
}
export default new SampleActionDispatcher();
Created a store which has a registered a callback to the dispatchersamplestore.js
import { EventEmitter } from 'events';
import Dispatcher from '../actiondispatchers/dispatcher';
import ActionType from '../actiondispatchers/actiontype';
// Constants
const SAMPLE_EVENT = 'SampleEvent';
class SampleStore extends EventEmitter {
constructor() {
super();
// Registering the callback
Dispatcher.register(this.dipatcherCallback.bind(this));
}
dipatcherCallback(action) {
switch (action.actionType) {
case ActionType.SAMPLE_ACTION:
this.emit(SAMPLE_EVENT);
break;
}
}
}
export default new SampleStore();
Here is the component that fires the action samplecomponent.js
import React from 'react';
import { StyleSheet,
Dimension,
TextInput,
View,
Text,
Button,
KeyboardAvoidingView } from 'react-native';
import SampleActionDispacther from '../../actiondispatchers/sampleactiondispatcher';
let screenWidth = Dimensions.get('window').width;
export default class SampleComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
textValue: ''
};
}
render() {
return (
<KeyboardAvoidingView id='sampleComponentHolder' style={styles.editorContainer} behavior='padding' enabled>
<View id='formFieldsHoldder' style={{paddingTop: 20}}>
<TextInput
id='textField1'
style={[styles.textStyle, styles.input]}
underlineColorAndroid = 'transparent'
placeholderTextColor='rgba(14,194,145,1)'
placeholder='Enter something'
keyboardType='numeric'
value={this.state.textValue}
onChangeText={(text) => this.setState({textValue: text})}
/>
<Button
id='saveButton'
color='rgba(46,107,138,1)'
title='Save'
onPress={this.onPressingSaveButton}
/>
</View>
</KeyboardAvoidingView>
);
}
/**
* On pressing the save button
*/
onPressingSaveButton =() => {
if (this.state.textValue !== '') {
SampleActionDispacther.saveSomething(Number(this.state.textValue));
} else {
alert('Oops! I dont think you have provided any values for saving.');
}
}
}
// Styles
const styles = StyleSheet.create({
editorContainer: {
flexDirection: 'column',
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
textStyle: {
color: 'rgba(14,194,145,1)',
fontFamily: 'monospace',
fontSize: 16,
textAlign: 'center'
},
input: {
height: 40,
width: (90 * screenWidth)/100,
borderWidth: 1
}
});
The real problem which makes me think that Flux is not working with react native is that, whenever I click the save button the dispatched action is not reaching the store. It would be really helpful if anyone can figure out why this is happening. Or is anything wrong with the code that I 've written?

Cannot read property 'oneOfType' of undefined

I am new to react native. I want to use 'react-native-camera'. I created a new project , installed the package correctly(I know because I have done it thrice to make sure that i'm not doing anything wrong) and it shows this error **Cannot read property 'oneOfType' of undefined ** and it is in index.js of this package 'react-native-camera'. I can't find any solution. I have tried changing gradle version, gradle wrapper properties everything that I could but this issue is not related to gradle. This is the code in my App.js. I know code isn't generating this error but i am new to react-native so maybe i'm missing something.
Any suggestion would be appreciated
import React, {Component} from 'react';
import {
Text,
View,
StyleSheet
} from 'react-native';
import Camera from 'react-native-camera';
export default class BarcodeScan extends Component {
constructor(props) {
super(props);
this.state = {
qrcode: ''
}
}
onBarCodeRead = (e) => this.setState({qrcode: e.data});
render () {
return (
<View style={styles.container}>
<Camera
style={styles.preview}
onBarCodeRead={this.onBarCodeRead}
ref={cam => this.camera = cam}
aspect={Camera.constants.Aspect.fill}
>
<Text style={{
backgroundColor: 'white'
}}>{this.state.qrcode}</Text>
</Camera>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'row',
},
preview: {
flex: 1,
justifyContent: 'flex-end',
alignItems: 'center'
}
});
Thanks to this guy , https://github.com/dwicao/react-native-panel/issues/4
I was able to solve this issue by replacing following in index.js of package 'react-native-camera'.
Replace
import React, { Component, PropTypes } from 'react';
with
import React, { Component } from 'react';
import PropTypes from 'prop-types';
In case someone finds this thread by searching on the the title (which I did), I will add my experience. Sometimes with TSX, imports get a little weird. What worked for me was using this import:
import * as PropTypes from 'prop-types'
I also need to do the same thing with React itself or it does not work either:
import * as React from 'react'
So far, these are the only two imports I need to do this for.