I converted this application
https://codesandbox.io/s/3y77o7vnkp <--Please check this link
into my react-native app and it works perfect. Since I implemented redux and redux-thunk I have a problem with fetching data.
There is a problem. I converted normal functions from this upper link to actions and reducers in react-native. For Example
handleSelect = itemValue => {
this.setState(
{
...this.state,
base: itemValue,
result: null,
},
this.calculate
);
};
TO actiontypes.js
export const HANDLE_FIRST_SELECT = 'HANDLE_FIRST_SELECT';
action.js
export const handleFirstSelect = itemValue => {
return {
type: actionTypes.HANDLE_FIRST_SELECT,
itemValue: itemValue,
};
};
and reducer.js
const initialState = {
currencies: ['USD', 'AUD', 'SGD', 'PHP', 'EUR', 'PLN', 'GBP'],
base: 'EUR',
amount: '',
convertTo: 'PLN',
result: '',
date: '',
error: null,
loading: false,
};
const exchangeCurrencies = (state = initialState, action) => {
switch (action.type) {
case actionTypes.HANDLE_FIRST_SELECT:
return {
...state,
base: action.itemValue,
result: null,
};
...
Next step I used mapStateToProps and mapDispatchToProps in my component like this
const mapDispatchToProps = dispatch => {
return {
handleFirstSelect: itemValue =>
dispatch(exchangeCurriencesActions.handleFirstSelect(itemValue)),
...
const mapStateToProps = (state) => {
return {
base: state.base,
amount: state.amount,
convertTo: state.convertTo,
result: state.result,
date: state.date,
};
};
And I'm using now this.props
<PickerComponent
selectedValue={this.props.base}
onValueChange={this.props.handleFirstSelect}
/>
Until then, everything works ok. Now when I download data in this way with react-redux and redux-thunk (action.js) it stops working
export const fetchDataSuccess = data => {
return {
type: actionTypes.FETCH_DATA_SUCCESS,
data: data,
};
};
export const fetchDataFail = error => {
return {
type: actionTypes.FETCH_DATA_FAIL,
error: error,
};
};
export const fetchData = () => {
return dispatch => {
fetch(`https://api.exchangeratesapi.io/latest?base=${this.props.base}`)
.then(res => res.json())
.then(
data => dispatch(fetchDataSuccess(data.rates)),
e => dispatch(fetchDataFail(e)),
);
};
};
next reducer.js
...
case actionTypes.FETCH_DATA_BEGIN:
return {
...state,
loading: true,
error: null,
};
case actionTypes.FETCH_DATA_SUCCESS:
console.log('data', action.data);
return {
...state,
date: action.data.date,
result: action.data.rates,
loading: false,
};
case actionTypes.FETCH_DATA_FAIL:
console.log('data', action.error);
return {
...state,
loading: false,
error: action.error,
};
...
In the next step added function fetchData into mapDispatchToProps and call this in componentDidMount like that
componentDidMount() {
if (this.props.amount === isNaN) {
return;
} else {
try {
this.props.fetchData();
} catch (e) {
console.log('error', e);
}
}
}
and finnaly I add calculations for currencies in mapStateToProps. I change result like that
result: (state.result[state.convertTo] * state.amount).toFixed(4),
and also I added applymiddleware into the store.
AND FINNALY THERE IS A ERROR
import React from 'react';
import HomeContentContainer from '../../containers/HomeContentContainer/HomeContentContainer';
class HomeScreen extends React.Component {
render() {
return <HomeContentContainer />;
}
}
export default HomeScreen;
Anyone know how to resolve this problem? Where and what should I change the code?
at HomeScreen.js line 6
) is not actually in the code you posted, but it definitely has something to do withresult: (state.result[state.convertTo] * state.amount).toFixed(4),
. You are trying to access a property onstate.result
which is initialized as a string...result: '',
in map state to props. – Henry Mueller