简体   繁体   English

如何使用reducer中的redux特别在react-native中获取API?

[英]How to fetch API in react-native specially using redux in the reducer?

in react native state is commonly used and really important if we have using data to manipulate or displaying in our phone, react native have a problem with state of course, but redux came to solve that struggle stuff, I'm still newbie guy in react native, and just understand how to fetch API in 'old' way not using redux, but how ? 反应本机状态是常用的,如果我们使用数据操作或显示在我们的手机中非常重要,当然有反应原生状态有问题,但是redux来解决那些挣扎的东西,我还是新手本机,只是了解如何以“旧”方式获取API而不使用redux,但是如何? in reducer I'm trying to call my function and callback in my component, but didn't work, here is the code : 在reducer中我试图在我的组件中调用我的函数和回调,但是没有用,这里是代码:

peopleReducer.js : peopleReducer.js:

import { ADD_FRIENDS } from "../types";
import { INIT_PEOPLE } from "../states";

const peopleReducers = (state = INIT_PEOPLE, action) => {
  switch (action.type) {
    // // save image
    case ADD_FRIENDS:

      // find how to fetch API in react native redux
      makeRemoteRequest = () => {
        const { page, seed } = state;
        const url = `https://randomuser.me/api/?seed=${seed}&page=${page}&results=20`;
        setState({ loading: true });
        fetch(url)
          .then(res => res.json())
          .then(res => {
            setState({
              data: page === 1 ? res.results : [...state.data, ...res.results],
              error: res.error || null,
              loading: false,
              refreshing: false
            });
            console.warn(res.results);
          })
          .catch(error => {
            setState({ error, loading: false });
          });
      };

    default:
      return state;
  }
};

export default peopleReducers;

friends.js friends.js

import React from "react";
import { View, Text, FlatList, ActivityIndicator } from "react-native";
import { ListItem, SearchBar } from "react-native-elements";

// connect to redux
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
// import actions
import { addFriends } from "../config/redux/actions/peopleActions";

class friends extends React.Component {
  componentDidMount() {
    this.props.addFriends();
    // this.makeRemoteRequest();
  }

  render() {
    //   console.warn(this.props.peopleListDashboard.data)
    return (
      <View>
        <FlatList
          data={this.props.peopleListDashboard.data}
          renderItem={({ item }) => (
                <ListItem leftAvatar={{ uri: item.picture.thumbnail }} />
          )}
          keyExtractor={item => item.email}
        />
      </View>
    );
  }
}

// make accesible publicDashboard properties from reducer
const mapStateToProps = state => {
  const { peopleListDashboard } = state;
  return { peopleListDashboard };
};

const mapDispatchToProps = dispatch =>
  bindActionCreators(
    {
      addFriends
    },
    dispatch
  );

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(friends);

about the trigger, yes I'm using action peopleAction.js 关于触发器,是的我正在使用动作peopleAction.js

import { ADD_FRIENDS } from "../types";

export const addFriends = friendsIndex => ({
    type: ADD_FRIENDS,
    payload: friendsIndex
});

hopefully you guys give me some clue, advice, even the answer is the good one for me. 希望你们给我一些线索,建议,即使答案对我来说也是好的。 Thank You 谢谢

I think you need to fetch your data in an action file, not in reducer, and then dispatch your response on the reducer and save it on the store. 我认为您需要在动作文件中获取数据,而不是在reducer中,然后在reducer上调度您的响应并将其保存在商店中。 with this approach, you can call your action whenever you want and this must work for you.i suggest checking these links too : 使用这种方法,您可以随时调用您的操作,这必须适合您。我建议您也检查这些链接:

https://redux.js.org/introduction/getting-started https://redux.js.org/introduction/getting-started

https://stackoverflow.com/questions/43848257/where-to-put-business-logic-in-redux-action-or-store https://stackoverflow.com/questions/43848257/where-to-put-business-logic-in-redux-action-or-store

this links maybe help you. 这个链接可能会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM