简体   繁体   English

ReactJs-下载前如何完成onClick-href

[英]ReactJs - How to complete onClick before download - href

I have a simple React button component that when clicked should retrieve and download data on the client browser. 我有一个简单的React按钮组件,单击该组件应在客户端浏览器上检索和下载数据。 The problem I am experiencing is that the download is triggered and the csv file downloaded before the data is passed into the href. 我遇到的问题是在将数据传递到href之前触发了下载并下载了csv文件。

Here is my component: 这是我的组件:

import { Component } from 'react';
import { connect } from 'react-redux';
import { PropTypes } from 'prop-types';
import { ManageUsersSelectors } from 'selectors/Users';
import { BatchRoleActions } from 'actions/Users';

 class UsersExportButton extends Component {
  constructor() {
    super();
    this.state = {
      users: ''
    };
  }

  getUsers(){
    const { userIds } = this.props;
    BatchRoleActions.getAllRoleUsers(userIds)
    .then((users) => {
      this.setState({ users: users});
      return  this.state.users;
    });
  }

  render() {
    return (
      <div className="roles-export-button">
        <a className="button button-default" href={this.state.users} download={'roles.csv'} onClick={() => this.getUsers()} return true>Export Csv</a>
      </div>
    );
  }
}

function mapStateToProps(state) {
  const userIds = ManageUsersSelectors.batchUserIdsSelector(state);
  return {
    userIds: userIds
  };
}

UsersExportButton.propTypes = {
   text: PropTypes.string.isRequired,
   data: PropTypes.array
};

export default connect(mapStateToProps)(UsersExportButton);

How can I get the getUsers()/onClick function to complete the data retrieval step before downloading? 在下载之前,如何获取getUsers()/ onClick函数以完成数据检索步骤?

When i debug my code I can see that the getUsers function returns data - however after the download is triggered 当我调试代码时,我可以看到getUsers函数返回数据-但是触发下载后

Make sure to bind this to your functions. 确保this绑定到您的函数。 In your constructor you can do: 在构造函数中,您可以执行以下操作:

 constructor() {
    super();
    this.state = {
      users: ''
    };

    this.getUsers = this.getUsers.bind(this);
  }

or you can use the bind this function: 或者您可以使用bind this功能:

  getUsers = () => {
    const { userIds } = this.props;
    BatchRoleActions.getAllRoleUsers(userIds)
    .then((users) => {
      this.setState({ users: users});
      return  this.state.users; // This should be removed, you can use this.state.users throughout this component.
    });
  }

Why not get the user data in the componentDidMount lifecycle method? 为什么不通过componentDidMount生命周期方法获取用户数据? It doesn't look like it needs to be called onClick. 看起来好像不需要调用onClick。

{
  // ...
  componentDidMount() {
    this.getUsers();
  }
  // ...
  render() {
    return (
      <div className="roles-export-button">
        <a className="button button-default" href={this.state.users} download={'roles.csv'}>Export Csv</a>
      </div>
    )
  }
}

How about handling the default "link" behaviour manually to get more control? 如何手动处理默认的“链接”行为以获得更多控制权? Also you should probably try to access state after setState has been executed via its callback. 同样,您可能应该在setState通过其回调执行后尝试访问state

eg 例如

getUsers(cb){
  const { userIds } = this.props;
  BatchRoleActions.getAllRoleUsers(userIds)
  .then((users) => {
    // note the callback of setState which is invoked
    // when this.state has been set
    this.setState({ users: users }, cb);
  });
}

const handleClick = () => {
  this.getUsers(() => {
    window.open(this.state.whatever)
  })
}

<span onClick={handleClick}>Export Csv</span>

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

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