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;