首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用react-redux和react-thunk将React应用程序转换为react-native - problem

使用react-redux和react-thunk将React应用程序转换为react-native - problem
EN

Stack Overflow用户
提问于 2020-03-07 00:12:55
回答 1查看 73关注 0票数 0

我将此应用程序https://codesandbox.io/s/3y77o7vnkp <--Please check this link转换为我的react原生应用程序,它工作得很完美。因为我实现了redux和redux-thunk,所以我在获取数据时遇到了问题。

这是一个问题。我将正常函数从上面的链接转换为react-native中的actions和reducers。例如

代码语言:javascript
复制
handleSelect = itemValue => {
    this.setState(
      {
        ...this.state,
        base: itemValue,
        result: null,
      },
      this.calculate
    );
  };

转到actiontypes.js

export const HANDLE_FIRST_SELECT = 'HANDLE_FIRST_SELECT';

action.js

代码语言:javascript
复制
export const handleFirstSelect = itemValue => {
  return {
    type: actionTypes.HANDLE_FIRST_SELECT,
    itemValue: itemValue,
  };
};

和reducer.js

代码语言:javascript
复制
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,
      };
...

下一步,我在组件中使用了mapStateToProps和mapDispatchToProps,如下所示

代码语言:javascript
复制
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,
  };
};

我现在使用的是this.props

代码语言:javascript
复制
<PickerComponent
    selectedValue={this.props.base}
    onValueChange={this.props.handleFirstSelect}
/>

在此之前,一切都很正常。现在,当我使用react-redux和redux-thunk (action.js)以这种方式下载数据时,它停止工作

代码语言:javascript
复制
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)),
      );
  };
};

下一个reducer.js

代码语言:javascript
复制
...
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,
      };
...

在下一步中,将函数fetchData添加到mapDispatchToProps中,并在componentDidMount中像这样调用它

代码语言:javascript
复制
  componentDidMount() {
    if (this.props.amount === isNaN) {
      return;
    } else {
      try {
        this.props.fetchData();
      } catch (e) {
        console.log('error', e);
      }
    }
  }

最后,我在mapStateToProps中添加了货币的计算。我像这样修改结果result: (state.result[state.convertTo] * state.amount).toFixed(4),

此外,我还将applymiddleware添加到商店中。

最后出现了一个错误

代码语言:javascript
复制
import React from 'react';
import HomeContentContainer from '../../containers/HomeContentContainer/HomeContentContainer';

class HomeScreen extends React.Component {
  render() {
    return <HomeContentContainer />;
  }
}

export default HomeScreen;

有人知道如何解决这个问题吗?我应该在哪里和什么地方更改代码?

EN

回答 1

Stack Overflow用户

发布于 2020-03-07 02:32:21

跟进对问题的评论,请务必查看这些评论...

mapStateToProps中保护您对state.result的访问,可能如下所示:

result: state.result ? (state.result[state.convertTo] * state.amount).toFixed(4) : null

,其中null也可以是0或其他什么,由您决定。在mapStateToProps请求完成之前,您的API确实会被调用(至少)一次,因此它需要处理state.result的初始状态。

我发现组件属性resultstate.result是不同的东西,这很令人困惑,但这只是我对命名的看法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60567842

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档