React Native,Fetch和Dispatch:如何在mapDispatchToProps()中访问responseData?

时间:2018-02-28 16:52:16

标签: react-native react-redux fetch dispatch

我从我的" Scanner"中获取componentWillMount()中的JSON数据。像这样的组件:

    program filesOut

    implicit none

    integer :: i, j
    real*8  :: refjd(64285), pha(64285)
    real*8  :: timejd(55436) 
    real*8  :: phs

100 format(47x,f10.2)
    open(15, file="neic56.out", status='old')
    do j=1,55436
       read(15,100) timejd(j)
    end do
    close(15)

200 format(f10.2,1x,f8.4)
    open(25,file="74-17.out", status='old')
    do i=1,64285
       read(25,200) refjd(i), pha(i)
    end do
    close(25)

300 format(f6.4)
    open(35,file="ejplphase.dat", status='unknown')

    outerloop: do i = 1, size(timejd)
       do j = 1, size(refjd)
          if (timejd(i) == refjd(j)) then
             phs = pha(j)/360.
             write(35,300) phs
             cycle outerloop
          end if
       end do
    end do outerloop
    close(35)
    end program

以下是我的发货代码:

async componentWillMount() {
  const url = 'https://foo.com/products/product1.json';

  fetch(url)
  .then((response) => response.json())
  .then((responseData) => this.props.dispatchProductLoad())
  .catch(error => {
    console.log(error);
  });
}

变量SOMEDATA应保存来自responseData的值(此时它尚未定义)

所以我的问题是 - 如何将SOMEDATA的值设置为responseData中保存的值?

1 个答案:

答案 0 :(得分:1)

您可以使用responseData作为参数调用操作创建者,并定义您的mapDispatchToProps函数,如

async componentWillMount() {
  const url = 'https://foo.com/products/product1.json';

  fetch(url)
  .then((response) => response.json())
  .then((responseData) => this.props.dispatchProductLoad(responseData))
  .catch(error => {
    console.log(error);
  });
}


function mapDispatchToProps(dispatch) {
  return { dispatchProductLoad: (response) => dispatch(productLoad(response)) };
};

export default connect(null, mapDispatchToProps)(Scanner);