I'm creating this component for fetch data from "https://facebook.github.io/react-native/movies.json" file. I am trying this as a testing app and later planned to use my actual API endpoint. However, i'm struggling to show data in the screen. It doesn't give any error or show errors.How can i add activity Indicator to check whether data is fetching and show data in ?
Action
import {
FETCHING_ASSISTANT_REQUEST,
FETCHING_ASSISTANT_SUCCESS,
FETCHING_ASSISTANT_FAILURE
} from "./types";
export function fetchAssistantRequest() {
return {
type: FETCHING_ASSISTANT_REQUEST
};
}
export function fetchAssistantFailure(errorMessage) {
return {
type: FETCHING_ASSISTANT_FAILURE,
errorMessage
};
}
export const fetchAssistant = () => {
return async dispatch => {
dispatch(fetchAssistantRequest());
try {
let response = await fetch(
"https://facebook.github.io/react-native/movies.json"
);
let json = await response.json();
dispatch(fetchAssistantSuccess(json.results));
} catch (error) {
dispatch(fetchAssistantFailure(error));
}
};
};
Reducer
import {
FETCHING_ASSISTANT_REQUEST,
FETCHING_ASSISTANT_SUCCESS,
FETCHING_ASSISTANT_FAILURE
} from "../actions/types";
//initial states
const initialState = {
isFetching: false,
errorMessage: null,
assistant: []
};
export default function assistantReducer(state = initialState, action) {
switch (action.type) {
//fetching state
case FETCHING_ASSISTANT_REQUEST:
return { ...state, isFetching: true };
//success state
case FETCHING_ASSISTANT_SUCCESS:
return { ...state, isFetching: false, assistant: action.data };
//faliuer state
case FETCHING_ASSISTANT_FAILURE:
return { ...state, isFetching: false, errorMessage: action.errorMessage };
default:
return state;
}
}
App Reducer
import .. from ".."
const AppReducer = combineReducers({
auth: AuthReducer,
// assis: AssistanceReduer,
// assistant: DumyReducer,
assis: AssisReducer
});
export default AppReducer;
App
import React, { Component } from "react";
import { View, Text, StyleSheet } from "react-native";
import { connect } from "react-redux";
import { fetchAssistant } from "../actions/AssistantAction";
class Assistant extends React.Component {
componentDidMount() {
this.props.getAssistance;
}
render() {
return (
<View>
<Text> {this.props.assistant.assistant.title}</Text>
<Text>{JSON.stringify(this.props.assistant.assistant.title)}</Text>
</View>
);
}
}
function mapStateToProps(state) {
return {
assistant: state.assis
};
}
function mapDispatchToProps(dispatch) {
return {
getAssistance: () => dispatch(fetchAssistant())
};
}
export default connect(
mapStateToProps,
mapDispatchToProps
)(Assistant);
STORE
import { createStore, applyMiddleware, compose } from "redux";
import { composeWithDevTools } from "remote-redux-devtools";
import thunk from "redux-thunk";
import AppReducer from "../reducers/AppReducer";
const store = createStore(
AppReducer,
{}, // initial state
composeWithDevTools(applyMiddleware(thunk))
);
export default store;
This is an untested solution, but it should work. To show an activity indicator you have to use the isFetching variable in your redux store. Since "assistant" is an array, you have to use map() it to show every single item. A better solution would be a FlatList, of course.
import React, { Component } from "react";
import { View, Text, StyleSheet, ActivityIndicator } from "react-native";
import { connect } from "react-redux";
import { fetchAssistant } from "../actions/AssistantAction";
class Assistant extends React.Component {
componentDidMount() {
this.props.getAssistance();
}
render() {
if(this.props.isFetching) return (<ActivityIndicator size="small" color="#00ff00" />);
return (
<View>
{this.props.assistant.assistant.map(item => <Text key={item.id}>{item.title}</Text>)}
</View>
);
}
}
function mapStateToProps(state) {
return {
isFetching: state.isFetching,
assistant: state.assis
};
}
function mapDispatchToProps(dispatch) {
return {
getAssistance: () => dispatch(fetchAssistant())
};
}
export default connect(
mapStateToProps,
mapDispatchToProps
)(Assistant);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With