Fetching data from API in react native using redux - react-native

I am working on a react-native project where I am trying to fetch data from an api using axios library and display the data.So,my app first shows a splash screen and it then needs to navigate to a page which consists of tabs.The tabs will contain the data from the api.
So,I am trying to initialize my store in my Homepage which comes after the splash screen.I have my reducer and action defined separately in 2 different files.
App.js file
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { StackNavigator } from 'react-navigation';
import SplashScreen from './src/components/SplashScreen';
import HomeScreen from './src/components/HomeScreen';
const Navigation = StackNavigator({
Splash: {
screen: SplashScreen
},
Home: {
screen: HomeScreen
}
})
export default Navigation;
My SplashScreen component:
import React from 'react';
import { StyleSheet,
Text,
View,
} from 'react-native';
export default class SplashScreen extends React.Component {
static navigationOptions = {
header: null
}
componentWillMount() {
setTimeout(() => {
this.props.navigation.navigate('Home')
},2000)
}
render() {
return(
<View style={styles.container}>
<Text style={styles.welcome}>Splash Screen</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'skyblue'
},
welcome: {
color: '#FFF',
fontSize: 30
}
})
My HomeScreen component:
import React from 'react';
import { StyleSheet,
Text,
View,
} from 'react-native';
export default class SplashScreen extends React.Component {
static navigationOptions = {
header: null
}
componentWillMount() {
setTimeout(() => {
this.props.navigation.navigate('Home')
},2000)
}
render() {
return(
<View style={styles.container}>
<Text style={styles.welcome}>Splash Screen</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'skyblue'
},
welcome: {
color: '#FFF',
fontSize: 30
}
})
import React from 'react';
import { StyleSheet,
Text,
View,
} from 'react-native';
export default class SplashScreen extends React.Component {
static navigationOptions = {
header: null
}
componentWillMount() {
setTimeout(() => {
this.props.navigation.navigate('Home')
},2000)
}
render() {
return(
<View style={styles.container}>
<Text style={styles.welcome}>Splash Screen</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'skyblue'
},
welcome: {
color: '#FFF',
fontSize: 30
}
})
Action.js file
import axios from 'axios';
export const FETCH_DATA = 'fetch_data';
const API = 'https://api.myjson.com/bins/fz62x';
export function fetchData() {
const request = axios.get(API);
return dispatch => {
return request.then((data) => {
dispatch({
type: FETCH_DATA,
payload: data
})
})
}
}
My reducer
import { FETCH_DATA } from './actions';
export default function(state={}, action) {
switch(action.type) {
case FETCH_DATA:
return {
...state,
action.payload
};
default:
return state;
}
}
Can anyon please tell me if this the correct way? If not, then what is the correct way?

I would say there is no correct or incorrect way to do this. But I can share kind of a pattern that I usually use.
First I would create separate folders for different files. Actions in actions folder, reducers in reducers folder etc... I would create separate constants.js file and configureStore.js file and put them inside the project root directory.
I would drop Axios library and would just use Fetch API for data fetching. Considering your code I would do the following.
Create configureStore.js file inside your project root directory. I recommend you to use Redux-Thunk. You can find more info from here.
configureStore.js
import { createStore, applyMiddleware } from 'redux';
import app from './reducers';
import thunk from 'redux-thunk';
export default function configureStore() {
let store = createStore(app, applyMiddleware(thunk))
return store
}
App.js
import React, { Component } from 'react';
import { Provider } from 'react-redux';
import { StackNavigator } from 'react-navigation';
import configureStore from './configureStore';
import HomeScreen from './screens/HomeScreen';
const NavigationApp = StackNavigator({
HomeScreen: { screen: HomeScreen }
})
const store = configureStore()
export default class App extends Component {
render() {
return (
<Provider store={store}>
<NavigationApp />
</Provider>
);
}
}
Let's create constants.js and we place it inside the project root directory.
constants.js
export const FETCHING_TODOS = 'FETCHING_TODOS';
export const FETCH_TODOS_SUCCESS = 'FETCH_TODOS_SUCCESS';
export const FETCH_TODOS_FAILURE = 'FETCH_TODOS_FAILURE';
Now let's move on and create our action file which would be put inside the actions folder. Let's name it something like fetchToDos.js. Let's create a simple function using Fetch API.
fetchToDos.js
import { FETCH_TODOS_SUCCESS, FETCH_TODOS_FAILURE, FETCHING_TODOS } from '../constants';
export function fetchToDos() {
return (dispatch) => {
dispatch(getTodos())
return(fetch('https://api.myjson.com/bins/fz62x'))
.then(res => res.json())
.then(json => {
return(dispatch(getToDosSuccess(json)))
})
.catch(err => dispatch(getToDosFailure(err)))
}
}
function getToDos() {
return {
type: FETCHING_TODOS
}
}
function getToDosSuccess(data) {
return {
type: FETCH_TODOS_SUCCESS,
data
}
}
function getToDosFailure() {
return {
type: FETCH_TODOS_FAILURE
}
}
fetchToDos.js with Axios
import { FETCH_TODOS_SUCCESS, FETCH_TODOS_FAILURE, FETCHING_TODOS } from '../constants';
import axios from 'axios';
export function fetchToDos() {
return (dispatch) => {
dispatch(getUser())
axios.get('https://api.myjson.com/bins/fz62x')
.then(function (response) {
// handle your response here, create an object/array/array of objects etc...
// and return it in dispatch(getToDosSuccess(data over here))
return(dispatch(getToDosSuccess(response.data)))
})
.catch(err => dispatch(getToDosFailure(err)))
}
}
// rest is the same...
Moving on to reducers. Let's create two files - index.js, todos.js and put them inside reducers folder.
todos.js
import { FETCH_TODOS_SUCCESS, FETCH_TODOS_FAILURE, FETCHING_TODOS } from '../constants';
const initialState = {
todos: [],
isFetching: false,
error: false
}
export default function todosReducer(state = initialState, action) {
switch(action.type) {
case FETCHING_TODOS:
return {
...state,
isFetching: true
}
case FETCH_TODOS_SUCCESS:
return {
...state,
isFetching: false,
todos: action.data
}
case FETCH_TODOS_FAILURE:
return {
...state,
isFetching: false,
error: true
}
default:
return state
}
}
index.js
import { combineReducers } from 'redux';
import todos from './todos';
const rootReducer = combineReducers({
todos
})
export default rootReducer
Basically "heavy lifting" is done. I would only create one screen because let's assume that users would tap back button (Android) while at home screen, they would end up on that splash screen. So in this example I am going to use one screen only.
HomeScreen.js
import React, { Component } from 'react';
import {
View,
Text,
ActivityIndicator
} from 'react-native';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { fetchTodos } from '../actions/fetchTodos';
class HomeScreen extends Component {
componentDidMount() {
this.props.fetchTodos()
}
render() {
const { todos, isFetching } = this.props.todos
if (isFetching) {
return(
<View style={{flex: 1, flexDirection: 'column', alignItems: 'center', justifyContent: 'center'}}>
<ActivityIndicator size={'large'} />
</View>
)
} else {
return(
<View style={{flex: 1, flexDirection: 'column', alignItems: 'center', justifyContent: 'center'}}>
<Text>todos.length</Text>
</View>
)
}
}
}
function mapStateToProps(state) {
return {
todos: state.todos
}
}
function mapDispatchToProps(dispatch) {
return {
...bindActionCreators({ fetchTodos }, dispatch)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(HomeScreen)
I really hope that you find this concept useful because I can say from my experience that it helped me a lot when I first get started and it helped me to understand the whole concept of redux much better.
Sorry if there are any typos and errors. I was on a flow.

Related

Unable to move to required screen using react-navigation?

I want to move to a different screen when I have some AsyncStorage item for authFlow but when I am going to the next screen and construction is calling out the function it is not moving to the next screen.
I tried to use the basic method this.props.navigation.navigate('navigator_name'), but i think it's not moving as blank screen is appearing.
App.js
------
import React from 'react';
import { StyleSheet, View, Text } from 'react-native';
import { createSwitchNavigator, createAppContainer, } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
import AuthLoadingScreen from './screens/auth-loading-screen';
import WelcomeScreen from './screens/welcomeScreen';
import AppHomeScreen from './screens/app-home-screen';
const AuthStackNavigator = createStackNavigator({
welcome: WelcomeScreen
})
// export default
const TestContainer = createAppContainer(createSwitchNavigator({
AuthLoading: AuthLoadingScreen,
Auth: AuthStackNavigator,
App: AppHomeScreen,
},
{
initialRouteName: 'AuthLoading',
}
))
export default class App extends React.Component {
constructor () {
super()
this.state = {
loggedIn: 'false',
}
}
render() {
return(
<View style={styles.container}>
<TestContainer />
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
auth-loading-screen
-------------------
import React, { Component } from 'react';
import { View, Text, StyleSheet, ActivityIndicator, AsyncStorage } from 'react-native';
export default class AuthLoadingScreen extends Component {
constructor (props) {
super(props);
console.log('calling _loadApp')
this._loadApp();
}
_loadApp = async() => {
const userToken = await AsyncStorage.getItem('userToken');
console.log('getting value of token ', userToken, JSON.stringify(this.props.navigation.actions))
this.props.navigation.navigate(userToken ? 'App' : 'Auth')
}
render() {
return (
<View style={styles.container}>
<ActivityIndicator />
</View>
)
}
}
const styles = StyleSheet.create ({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
}
})
welcomeScreen
-------------
import React, { Component } from 'react';
import { View, Text, StyleSheet, Button} from 'react-native';
export default class WelcomeScreen extends Component {
constructor (props) {
super(props);
}
render() {
return (
<View style={styles.container}>
<Text>Welcome</Text>
</View>
)
}
}
const styles = StyleSheet.create ({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
}
})
The screen should directly move to the welcomeScreen as item is not set through AsynStorage
Your function is asynchronous. However, the constructor cannot execute the asynchronous function. Therefore, you should change the position of executing the function.
async componentDidMount() {
await this._loadApp();
}

How to get value from side menu with custom component based on drawer navigation

I faced with the following problem: there's my side navigation class:
import { createAppContainer, createDrawerNavigator } from 'react-navigation';
import { Dimensions } from 'react-native';
import MainTabNavigator from './MainTabNavigator';
import MapsScreen from '../screens/MapsScreen';
import DetailsScreen from '../components/DetailInfoScreen';
const DrawerNavigator = createDrawerNavigator(
{
Home: {
screen: MainTabNavigator
},
Maps: {
screen: DetailsScreen
}
}, {
contentComponent: SideMenu,
drawerWidth: Dimensions.get('window').width - 120,
}
);
export default createAppContainer(DrawerNavigator);
SideMenu.js:
import React, { Component } from 'react';
import { SafeAreaView, Switch, View } from 'react-native';
import { Text } from 'react-native-elements';
export default class SideMenu extends Component {
constructor(props) {
super(props);
this.state = {
switchPosition: false,
};
}
toggleSwitch = (value) => {
this.setState({ switchPosition: value });
};
render() {
const { switchPosition } = this.state;
return (
<SafeAreaView style={{ flex: 1 }}>
<View style={{ flex: 1, padding: 16 }}>
{switchPosition
? <Text>Switch to Celsius</Text>
: <Text>Switch to Kelvin</Text>
}
<Switch
style={{ marginTop: 20 }}
onValueChange={this.toggleSwitch}
value={switchPosition}
/>
</View>
</SafeAreaView>
);
}
}
As you can see, there's a switch in my custom component. And so, can you explain me, how can I get the value of this switch from, for example, DetailInfoScreen? Or there are no ways to do it?
The perfect solution would be to use Redux . You can set value in any component and can get value in any component.
If you do not want to use Redux - Short solution would be Define a Global variable and you can get and set value in any component
Create a Class DataHandler.js
let switchStatus;
function setSwitchStatus(status) {
switchStatus = status;
}
function getSwitchStatus() {
return switchStatus;
}
export default {
setSwitchStatus,
getSwitchStatus
};
Set Value in SideMenu
import { setSwitchStatus } from './DataHandler';
toggleSwitch = (value) => {
this.setState({ switchPosition: value });
DataHandler. setSwitchStatus(value);
};
Get Value in DetailInfoScreen
import { getSwitchStatus } from './DataHandler';
componentDidMount() {
const switchStatus = DataHandler.getSwitchStatus()
}

How to call mapStateToProps on each instance of a Component in React-Redux

I have a component called AuditionItem, multiple instances of which are added to the parent component called AuditionsList.
I have done export default connect(mapStateToProps)(AuditionItem)
From my experience mapStateToProps is called only for one instance of AuditionItem (the one that initiates the state change). But I want the mapStateToProps to be called for EACH instance of AuditionItem.
Is there a way to do this?
Here's my code for AuditionItem.js:
import React from 'react';
import { StyleSheet, Text, View, Image, TouchableOpacity } from 'react-native';
import { Ionicons } from '#expo/vector-icons';
import Moment from 'moment';
import colors from './../styles/colors';
import { store } from './../App';
import { addBookmark, removeBookmark } from './../actions/creators';
import { connect } from 'react-redux';
class AuditionItem extends React.Component {
_toggleBookmark = (auditionId, bookmarked) => {
if(bookmarked)
store.dispatch(removeBookmark(auditionId));
else
store.dispatch(addBookmark(auditionId));
}
render() {
Moment.locale('en');
let bookmarked = (this.props.auditions.indexOf(this.props.auditionId) > -1) ? true : false;
let roleString = String(this.props.role);
if(roleString.length > 35)
roleString = roleString.substring(0, 35) + " ...";
let projectString = String("Project: (" + this.props.productionType + ") " + this.props.project);
if(projectString.length > 35)
projectString = projectString.substring(0, 35) + " ...";
let productionHouseString = String("Production House: " + this.props.productionHouse);
if(productionHouseString.length > 35)
productionHouseString = productionHouseString.substring(0, 35) + " ...";
let iconName = `ios-bookmark${bookmarked ? '' : '-outline'}`;
return (
<View style={styles.auditionItemWithBookmark}>
<View style={styles.bookmark}>
<TouchableOpacity onPress={() => this._toggleBookmark(this.props.auditionId, bookmarked)} >
<Ionicons name={iconName} size={25} />
</TouchableOpacity>
</View>
<View style={styles.auditionItem}>
<Text style={styles.role}>{roleString}</Text>
<Text style={styles.project}>{projectString}</Text>
<Text style={styles.productionHouse}>{productionHouseString}</Text>
<Text style={styles.auditionDate}>Begins: {Moment(String(this.props.auditionDate).replace('"','').replace('"', '')).format('LLLL')}</Text>
</View>
</View>
)
}
}
const styles = StyleSheet.create({
auditionItemWithBookmark: {
flex: 1,
flexDirection: "row",
backgroundColor: colors.auditionItemBackgroundColor,
borderRadius: 10,
margin: 10,
padding: 15,
},
bookmark: {
flex: 1,
paddingTop: 5,
},
auditionItem: {
flex: 8,
flexDirection: "column",
backgroundColor: colors.auditionItemBackgroundColor,
},
role: { color: colors.auditionItemColor, fontSize: 20, fontWeight: "bold" },
project: { color: colors.auditionItemColor },
productionHouse: { color: colors.auditionItemColor },
auditionDate: { color: colors.auditionItemColor },
});
const mapStateToProps = state => {
return {
auditons: state.bookmarks.auditions,
}
}
export default connect(mapStateToProps)(AuditionItem);
And the code for the parent AuditionsList.js
import React from 'react';
import { Text, View, FlatList, ActivityIndicator } from 'react-native';
import { connect } from 'react-redux';
import AuditionItem from './AuditionItem';
import Auditions from './../data/Auditions';
import { store } from './../App';
class AuditionsList extends React.Component {
constructor(props) {
super(props);
this.state = { isLoading: true, data: [] }
}
componentDidMount() {
this._refreshData();
}
componentDidUpdate(prevProps) {
if((this.props.location !== prevProps.location) || (this.props.roleType !== prevProps.roleType))
this._refreshData();
}
_onRefresh() {
this.setState({ isLoading: true }, this._refreshData() );
}
_refreshData = () => {
Auditions.fetchAuditions(this.props.productionType, this.props.location, this.props.roleType).then(auditions => {
this.setState({ isLoading: false, data: this._addKeysToAuditions(auditions) });
});
}
_addKeysToAuditions = auditions => {
return auditions.map(audition => {
return Object.assign(audition, { key: audition.Role});
});
}
_renderItem = ({ item }) => {
return (
<AuditionItem
auditionId={item.objectId}
role={item.Role}
project={item.Project.Name}
productionType={item.Project.ProductionType.Type}
auditionDate={JSON.stringify(item.Date.iso)}
productionHouse={item.Project.ProductionHouse.Name}
auditions={store.getState().bookmarks.auditions}
/>
);
}
render() {
if (this.state.isLoading) {
return (
<View style={{flex: 1, paddingTop: 20}}>
<ActivityIndicator />
</View>
);
}
return (
<View style={{ flex: 1 }}>
<FlatList onRefresh={() => this._onRefresh()} refreshing={this.state.isLoading} data={this.state.data} renderItem={this._renderItem} />
</View>
);
}
}
const mapStateToProps = state => {
return {
location: state.settings.location,
roleType: state.settings.roleType,
};
}
export default connect(mapStateToProps)(AuditionsList);
Code for App.js:
import React from 'react';
import { Text, View, ActivityIndicator } from 'react-native';
import { Ionicons } from '#expo/vector-icons';
import { Header } from 'react-native-elements';
import { createMaterialBottomTabNavigator } from 'react-navigation-material-bottom-tabs';
import { SettingsDividerShort, SettingsCategoryHeader, SettingsPicker} from 'react-native-settings-components';
import BookmarksScreen from './screens/BookmarksScreen';
import AuditionsScreen from './screens/AuditionsScreen';
import SettingsScreen from './screens/SettingsScreen';
import { AsyncStorage } from "react-native";
import { createStore } from 'redux';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import rootReducer from './reducers/index';
import { Provider } from 'react-redux';
import { PersistGate } from 'redux-persist/lib/integration/react';
import { autoMergeLevel2 } from 'redux-persist/lib/stateReconciler/autoMergeLevel2';
const persistConfig = {
key: 'root',
storage: AsyncStorage,
stateReconciler: autoMergeLevel2,
whitelist: ['settings', 'bookmarks']
};
const pReducer = persistReducer(persistConfig, rootReducer);
export const store = createStore(pReducer);
export const persistor = persistStore(store);
const MaterialBottomTabNavigator = createMaterialBottomTabNavigator(
{
Bookmarks: BookmarksScreen,
Auditions: AuditionsScreen,
Settings: SettingsScreen,
},
{
shifting: true,
initialRouteName: 'Auditions',
barStyle: { backgroundColor: 'black' },
}
);
export default class App extends React.Component {
render() {
return (
<Provider store={store}>
<PersistGate loading={<ActivityIndicator />} persistor={persistor}>
<MaterialBottomTabNavigator />
</PersistGate>
</Provider>
)
}
}
This doesn't create an instance of the class, it just exports the class/object.
export default connect(mapStateToProps)(AuditionItem)
Export reference
You get the instance when you call the constructor of the class, but that's after importing it. So basically, all the time you use the imported AuditionItem (i.e: < AuditionItem />) React internally is creating a new instance of the class.
I guess the problem is either on AuditionItem itself, or the props you're passing to.
Additional information about mapStateToProps
from the official Redux documentation
[mapStateToProps(state, [ownProps]): stateProps] (Function): If this argument is specified, the new component will subscribe to Redux store updates. This means that any time the store is updated, mapStateToProps will be called. The results of mapStateToProps must be a plain object, which will be merged into the component’s props. If you don't want to subscribe to store updates, pass null or undefined in place of mapStateToProps.

react native navigation Could not find "store" in either the context or props of "Connect(facebookLogin)

I'm building react native app authentication with facebook login.
I'm using stack navigator and I want to add redux to my app.
I just seperate my files to components and when I use stack navigation with redux I get this error
Could not find "store" in either the context or props of
"Connect(facebookLogin)". Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect(facebookLogin)".
index.android.js
import React, { Component } from 'react';
import { Text, View, StyleSheet, TouchableOpacity, AppRegistry, Image } from 'react-native';
import facebookLogin from './src/components/FacebookLogin';
import Home from './src/components/Home';
import {
StackNavigator,
} from 'react-navigation';
const App = StackNavigator({
Home: { screen: facebookLogin },
HomePage: {screen:Home},
})
AppRegistry.registerComponent('facebookLogin', () => App);
FacebookLogin.js
/**
* Sample React Native App
* https://github.com/facebook/react-native
* #flow
*/
import React, { Component } from 'react';
import { Text, View, StyleSheet, TouchableOpacity, AppRegistry, Image } from 'react-native';
import FBSDK, { LoginManager, GraphRequest, GraphRequestManager } from 'react-native-fbsdk';
import Icon from 'react-native-vector-icons/FontAwesome';
// redux
import { Provider } from 'react-redux';
import { connect } from 'react-redux';
import { createStore } from 'redux';
import reducers from '../reducers/ProfileReducers';
const store = createStore(reducers, window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__());
class facebookLogin extends Component {
constructor(props) {
super(props);
this.state = {
result: null
}
}
_fbAuth() {
const { navigate } = this.props.navigation;
LoginManager.logInWithReadPermissions(['public_profile','user_birthday']).then((result) => {
if (result.isCancelled) {
console.log('Login was canclled');
}
else {
console.log(result);
const infoRequest = new GraphRequest(
'/me?fields=id,name,picture.width(800).height(800),gender,birthday,first_name,last_name',
null,
(error, result) => {
if (error) {
alert('Error fetching data: ' + error.toString());
} else {
console.log(result);
this.setState({ result });
navigate('HomePage');
}
}
);
// Start the graph request.
new GraphRequestManager().addRequest(infoRequest).start();
}
}, function (error) {
console.log('An error occured:' + error);
})
}
getProfileData() {
const { ImageStyle } = styles;
if (this.state.result) {
return (
<View>
{this.state.result.picture ? <Image style={ImageStyle} source={{ uri: this.state.result.picture.data.url }}></Image> : null}
<Text> first name: {this.state.result.first_name} </Text>
<Text> Last name: {this.state.result.last_name} </Text>
<Text> Gender {this.state.result.gender} </Text>
</View>
)
}
return null;
}
render() {
return (
<Provider store = {store}>
<View style={styles.container}>
<Icon.Button name="facebook" backgroundColor="#3b5998" onPress={() => { this._fbAuth() }}>
<Text style={{fontFamily: 'Arial', fontSize: 15, color:'white'}}>Login with Facebook</Text>
</Icon.Button>
{this.getProfileData()}
</View>
</Provider>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
ImageStyle:{
borderRadius:80,
width:100,
height:100
}
});
const mapStateToProps = (state) => {
return
{ profile: state.profile};
}
export default connect(mapStateToProps)(facebookLogin);
when it was only one component in index.android.js it works fine without any error but when I seperate it to some components using stack navigation I get this error.
my profileReducers
var profile = {
name :'',
email:'',
photo:''
}
const initialState = {
profile,
};
export default (state = initialState, action) => {
console.log("state");
switch (action.type) {
case 'INSERT_PROFILE_DETAILS':
return {
profile: action.payload
}
case 'GET_PROFILE_DETAILS': {
return state;
}
default:
return state;
}
}
Try setting up redux in index.android.js instead so it looks something like this:
// Set up redux store above
const Navigator = StackNavigator({
Home: { screen: facebookLogin },
HomePage: { screen: Home },
});
const App = () => (
<Provider store={store}>
<Navigator />
</Provider>
);
AppRegistry.registerComponent('facebookLogin', () => App);

react-navigation tab navigator Screens' contents are not shown / the screens are blank issue

I have a problem with react-navigation tab navigator. Screens' contents are not shown / the screens are blank. any ideas ?
This is my navigator structure:
import { StackNavigator, TabNavigator } from 'react-navigation';
import PeopleList from './PeopleList';
import CompanyList from './CompanyList';
import AddPerson from './AddPerson';
const Navigation = TabNavigator({
People: { screen: PeopleList },
Person: { screen: AddPerson },
Company: { screen: CompanyList },
}, {
tabBarOptions: {
activeTintColor: 'white',
inactiveTintColor: '#80cbc4',
swipeEnabled: true,
showLabel: false,
style: {
backgroundColor: '#26a69a',
},
},
});
export default Navigation;
UPDATE:
this the App.js file which include render methods:
import React, {Component} from 'react';
import {StyleSheet, Text, View} from 'react-native';
import firebase from 'firebase';
import {Provider} from 'react-redux';
import {createStore} from 'redux'
import Login from './Login';
import Loader from './Loader';
import Navigation from './Navigation';
import reducers from '../reducers/PeopleReducer';
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF'
}
});
const store = createStore(reducers);
export default class App extends Component {
state = {
loggedIn: null
};
componentWillMount() {
firebase.initializeApp({
apiKey: "xxxxxxxxxxxxxxxxxxx",
authDomain: "xxxxxxxxxxxxxxxxxxx",
databaseURL: "xxxxxxxxxxxxxxxxxxx",
projectId: "xxxxxxxxxxxxxxxxxxx",
storageBucket: "xxxxxxxxxxxxxxxxxxx",
messagingSenderId: "xxxxxxxxxxxxxxxxxxx"
});
firebase
.auth()
.onAuthStateChanged((user) => {
if (user) {
this.setState({loggedIn: true});
} else {
this.setState({loggedIn: false});
}
});
}
renderInitialView() {
switch (this.state.loggedIn) {
case true:
return <Navigation/> /*exists above*/
case false:
return <Login/>;
default:
return <Loader size="large"/>;
}
}
render() {
return (
<Provider store={store}>
<View style={styles.container}>
{this.renderInitialView()}
</View>
</Provider>
);
}
}
I'm sure it goes for the first case of switch, but got a blank screen and I don't know the reason.
render() {
return (
<Provider store={store}>
<View style={styles.container}>
{this.renderInitialView()}
</View>
</Provider>
);
}
}
remove the tag navigation cannot be wrapped in a tag , like this.
render() {
return (
<Provider store={store}>
{this.renderInitialView()}
</Provider>
);
}
}
this will work then.