【问题标题】:How to handle a second onClick to initiate an API request using reactJS and redux?如何处理第二个 onClick 以使用 reactJS 和 redux 发起 API 请求?
【发布时间】:2019-06-06 20:30:21
【问题描述】:

我是 redux 的新手,我正在创建一个小应用程序来呈现 API onClick。我调用 componentDidMount 中的动作创建者,它能够在第一次点击时生成所需的结果。但是,因为 componentDidMount 只渲染一次,所以在第二次“点击”时没有任何反应。

以下代码是在该函数内部包含名为“handleFavClick”的 onClick 的组件,触发了两个动作创建者,其中一个识别被单击的按钮并将其添加到状态以供第二个动作创建者使用,后者获取 API .

import React from 'react';
import { connect } from 'react-redux';
import '../stylesheet/FavoriteList.css';
import { uploadGif } from '../actions';

import Display from './Display';




class FavoriteList extends React.Component {


handleFavClick = (id) => {
    this.props.uploadGif(id);
    this.displayGif();
}

displayGif = () => {
    this.setState({display: true});
};


renderList = (callback) => {
    let fullList = this.props.favsList.map(function(cur, index) {
        return (
        <button onClick={function() {
            callback(index);
        }} className="list-item" key={index}>{cur}</button>
        )
    });
    return fullList;
}

render() {
    return (
        <div>
            <div className="favs">
                <h2>Favorite List</h2>
                <ul className="unordered-list">
                    {this.renderList(this.handleFavClick)}
                </ul>
            </div>
            {this.state.display && <Display />}
        </div>

    )
}
}

const mapDispatchToProps = {
uploadGif,

}

const mapStateToProps = (state) => {
return {
favsList: state.myFirstReduxKey,

}
};

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

显示组件(触发onClick后,渲染该组件,并使用从API请求接收到的数据更新显示)

import React from 'react';
import { connect } from 'react-redux';

import { getGif } from '../actions';

class Display extends React.Component {
    componentDidMount() {
        const list = this.props.gifList;
        const item = this.props.chosenGif;
        this.props.getGif(list[item]);
   }

.
.
.
.

const mapStateToProps = (state) => {
    return {
    gifList: state.myFirstReduxKey,
    chosenGif: state.uploadGif,
    gifs: state.getGif
    }
}


export default connect(mapStateToProps, { getGif })(Display);

调用动作创建者

export const getGif = (action) => async dispatch => {
    const key = 'GRghbyFwY5CEhc1h7ngS9KBEK9s2W3zBa'
    const response = await gifApi.get(`search?q=${action}&api_key=${key}`)
    .then(function(response) {
        return response.data.data
    });


    dispatch({ type: GET_GIF, payload: response})
};

归根结底,我想知道 redux 程序员如何处理客户端单击一个按钮,该按钮呈现某些内容,然后客户端单击一个新按钮,该按钮删除先前单击的呈现并呈现新信息。

【问题讨论】:

  • 具有“onclick”属性的组件在哪里可以调用该操作?请发布完整代码
  • 对不起,塔雷克。我刚看到这个。

标签: reactjs redux axios redux-thunk


【解决方案1】:

您的代码看起来不错,但是您需要更改一件事以使这项工作符合预期,就像您所说的那样,因为您的代码在 componentDidMount 中,这只会在 Creational 生命周期中工作 的组件, 现在你可以做两件事。

1.**将您的 this.props.getGif(list[item]) 移动到 **render()推荐):

 import React from 'react';
 import { connect } from 'react-redux';    
 import { getGif } from '../actions';

    class Display extends React.Component {
       render(){
            const list = this.props.gifList; //this code gets executed every time
            const item = this.props.chosenGif;
            this.props.getGif(list[item]);
       }
    }

    const mapStateToProps = (state) => {
       return {
        gifList: state.myFirstReduxKey,
        chosenGif: state.uploadGif,
        gifs: state.getGif
      }
    }


export default connect(mapStateToProps, { getGif })(Display);

2.有一个单独的处理程序并让componentDidMountcomponentDidUpdate 调用它:

import React from 'react';
import { connect } from 'react-redux';

import { getGif } from '../actions';

class Display extends React.Component {
   componentDidMount() {
        this.loadGifHandler();
   }

   componentDidUpdate() {
        this.loadGifHandler();
   }


   loadGifHandler = () => {
        const list = this.props.gifList;
        const item = this.props.chosenGif;
        this.props.getGif(list[item]);
   }

.
.
.
.

const mapStateToProps = (state) => {
    return {
    gifList: state.myFirstReduxKey,
    chosenGif: state.uploadGif,
    gifs: state.getGif
    }
}


export default connect(mapStateToProps, { getGif })(Display);

【讨论】:

  • 我正在尝试使用此解决方案。问题是 getGif() 是一个动作创建者,在运行后,它会重新渲染导致循环的组件。
  • getGif() 是异步动作创建者吗?
  • 是的。它使用 axios 从 API 返回 JSON 对象。
  • 我找到了答案。我放弃为 API 请求的结果创建一个单独的组件,只是在搜索栏组件内的 onClick 函数内启动并显示 API 请求。一切都发生在同一个组件中,但它有效。感谢您试用 Praveen,我会记住您的回答以备日后使用!
  • 乐于助人:)
猜你喜欢
  • 2021-04-02
  • 2019-09-02
  • 2016-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-30
相关资源
最近更新 更多