【问题标题】:Get JSON Data in multiple components using reactjs and redux使用 reactjs 和 redux 在多个组件中获取 JSON 数据
【发布时间】:2019-04-30 01:23:04
【问题描述】:

我想将来自单个 API 的数据显示给不同的组件,因为我只想访问 API 一次并将数据分发到多个小组件。我知道我可以通过使用 redux state 来做到这一点,但不知道该怎么做。需要您的帮助来实现这一目标。以下是到目前为止完成的代码。

主页/index.js

import SlidingBanner from './banner/BannerList';
import Celebslider from './celebrityslider/CelebSlider';

class HomePage extends Component {
        render() {
          return (
            <div>
                <SlidingBanner />
                <anotherslider />
            </div>  
          );
        }
      }

    export default HomePage;

BannerList.js

import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { itemsFetchData } from '../../../actions/items';

class BannerList extends Component {

    componentDidMount() {
        this.props.fetchData();
    }

    render() {
        let bannerArray = [];
        let banner = this.props.items.banner
        for (let key in banner) {
            bannerArray.push(banner[key]);
            return (
                <div>
            <Slider {...slidersettings}>
            {this.props.items.banner.map((item) => (
                    <div key={item.id}>
                        <img src={item.image_url} className="img-responsive"/>
                    </div>
                ))}
            </Slider>        
            </div>
            );
        }
        if (this.props.hasErrored) {
            return <p>Sorry! There was an error loading the items</p>;
        }

        if (this.props.isLoading) {
            return <p>Loading…</p>;
        }
        return (null);
    }
}

BannerList.propTypes = {
    fetchData: PropTypes.func.isRequired,
    items: PropTypes.object.isRequired,
    hasErrored: PropTypes.bool.isRequired,
    isLoading: PropTypes.bool.isRequired
};

const mapStateToProps = (state) => {
    return {
        items: state.items,
        hasErrored: state.itemsHasErrored,
        isLoading: state.itemsIsLoading
    };
};

const mapDispatchToProps = (dispatch) => {
    return {
        fetchData: (url) => dispatch(itemsFetchData(url))
    };
};

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

anotherslider.js

Now in this file, i want to fetch another array of objects or object from the same API. 

I tried to mount the API in container component but did not worked, I hope i am doing some mistake. Please correct.  

【问题讨论】:

标签: json reactjs api redux jsx


【解决方案1】:

如果您想在 anotherslider.js 文件中获取数据,您必须将 reducer 连接到其中的类/函数,以及在 BannerList.js 文件。 现在在渲染调用componentWillReceiveProps(nextProps) 函数之前,您将在此处获取数据。

【讨论】:

    【解决方案2】:

    如果你想在两个滑块中调用数据,你有两种方法来处理它。

    1. HomePage.js 组件中发出您的redux 请求并将数据绑定到其他组件。
    2. 当您获得BannerList.js 组件上的数据时,您的状态将被更新。只需将 redux 连接添加到您的 anotherslider.js 组件并在更新时获取数据。

      const mapStateToProps = (state) => {
          return {
              items: state.items,
              hasErrored: state.itemsHasErrored,
              isLoading: state.itemsIsLoading
          };
      };
      
      export default connect(mapStateToProps, mapDispatchToProps)(HomeList);
      

    【讨论】:

    • 感谢您的回复。你有第一个选项的例子吗? (在 HomePage.js 组件中发出你的 redux 请求并将数据绑定到其他组件。)??
    • @user1328423 我做了一个简单的例子,请检查一下。 codesandbox.io/s/4zo2mj09o7
    • 很抱歉再次打扰您。但是该示例显示错误。 Line 16: 'items' is not defined no-undef Line 16: 'hasErrored' is not defined no-undef Line 16: 'isLoading' is not defined no-undef Line 24: 'items' is not defined no-undef 这一行也有语法错误{items, hasErrored, isLoading} = this.props;
    • 好的,我能够在渲染方法中将其更改为以下内容,并且有效。 const {items, hasErrored, isLoading} = this.props;
    • @user1328423 很高兴为您提供帮助。
    【解决方案3】:

    除了所有这些选项之外,您还可以使用 react 的 Context API 作为 Provider/consumer 来在小组件之间分发您的数据......这将节省您将 props 传递给所有小组件并使用 Context 直接访问组件中的值。消费者 .. 此外,如果您不想将此状态存储在全局 redux 存储中,上下文 API 会将您从中拯救出来......

    【讨论】:

    • 感谢您的信息,我一定会看的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    • 2019-09-03
    • 1970-01-01
    • 2018-03-23
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多