简体   繁体   English

将道具传递给render方法中的组件?

[英]Pass props to component in render method?

I have a button component in my render method ( DownloadReportsButton ) that will trigger a modal onClick and perform an API call. 我的渲染方法( DownloadReportsButton )中有一个按钮组件,它将触发模式onClick并执行API调用。

The logic for the modal and the API call have already been moved to the button component, but how do I pass my data (an array called awsFileKeys ) to perform the API call to the DownloadReportsButton component? 模态和API调用的逻辑已经移到了按钮组件,但是如何传递我的数据(称为awsFileKeys的数组)以执行对DownloadReportsButton组件的API调用?

My view: 我的观点:

render() {

    const {reportJSON} = this.state; 

    const content = (      
      <div className="container">
        <div className="card mb-3">
        <div className="card-header">
              <div className="float-left">
                <i className="fas fa-list mr-1"></i>
                Threat Reports
              </div>
              <div className="float-right">
                <DownloadReportsButton />
              </div>
              </div>            
              <div className="card-body">
              <div className="table-responsive">
              <Busy isBusy={this.state.tableIsBusy}>
                  <ReactTable
                    data={reportJSON}
                    filterable
                    defaultFilterMethod={(filter, row) =>
                    String(row[filter.accessor]) === filter.value}
                    columns={[
                      {
                        id: "checkbox",
                        accessor: "",
                        Cell: ({ original }) => {
                          return (
                            <input
                              type="checkbox"
                              className="checkbox"
                              checked={this.state.selected[original.linkRaw] === true}
                              onChange={() => this.toggleRow(original.linkRaw)}
                            />
                          );
                        },
                        Header: x => {
                          return (
                              <input
                                  type="checkbox"
                                  className="checkbox"
                                  checked={this.state.selectAll === 1}
                                  ref={input => {
                                      if (input) {
                                          input.indeterminate = this.state.selectAll === 2;
                                      }
                                  }}
                                  onChange={() => this.toggleSelectAll()}
                              />
                          );
                      },
                        sortable: false,
                        width: 45
                      },
                      { Header: 'Date', 
                        accessor: 'date', 
                        maxWidth: 120,
                        filterMethod: (filter, rows) =>
                        matchSorter(rows, filter.value, { keys: ["date"] }),
                        filterAll: true},

                      { Header: 'Title', 
                        accessor: 'title',
                        filterMethod: (filter, rows) =>
                        matchSorter(rows, filter.value, { keys: ["title"] }),
                        filterAll: true},

                      { Header: 'Link', 
                        accessor: 'link', 
                        maxWidth: 120},
                    ]}
                    loading={this.state.tableIsBusy}
                  />
              </Busy>
              </div>
            </div>
            <div className="card-footer small text-muted">Updated yesterday at 11:59 PM</div>
          </div>
      </div>
    )

My button component: 我的按钮组件:

class DownloadReportsButton extends Component {
  constructor(props) {
    super(props);
    this.state = {
      isOpen: false,
    }
    this.openModal = this.openModal.bind(this);
    this.closeModal = this.closeModal.bind(this);
  }

  openModal(){
    this.setState({isOpen: true});
  }

  closeModal(){   
    this.setState({isOpen: false});
  }

  downloadMultipleReports(e){

    fetch(config.api.urlFor('downloadMultipleReports', { fileKeys: this.state.awsFileKeys }))
    .then((response) => response.json())

    this.closeModal();

  }

  render() {
    return (
      <div>
      <button type="button" className="btn btn-primary" style={{marginLeft: '10px'}} onClick={this.openModal}>Download Selected</button>  
      <Modal
          isOpen         = {this.state.isOpen}
          onRequestClose = {this.closeModal}
          style          = {modalStyle}
          contentLabel   = "Generate Report Input"
        >
          <form data-test-id='GenerateReport-modal-1'>
          <h4 style={{textAlign: "center", marginBottom: "15px"}}>Your files are downloading, please wait...</h4>
          </form>
        </Modal>
      </div>
    );
  }
}


export default DownloadReportsButton

If I am understanding your question soundly (please excuse me if it is not the case) you can pass props to the DownloadReportsButton component by setting attributes when you include it in your view render method. 如果我很好地理解了您的问题(如果不是这样,请原谅),您可以通过将属性包含在视图render方法中来设置属性,从而将属性传递给DownloadReportsButton组件。 Assuming that the array is a component of the state of your view, you can include DownloadReportsButton as follows: 假设数组是视图状态的组成部分,则可以包含DownloadReportsButton ,如下所示:

<DownloadReportsButton fileKeys={ this.state.awsFileKeys }/>

The DownloadReportsButton can access to this information as this.props.fileKeys . DownloadReportsButton可以作为this.props.fileKeys访问此信息。
In general, the attributes you specify when including a React component are accessed as component props . 通常,您在包含React组件时指定的属性将作为组件props访问。

I have chosen a prop name different than awsFileKeys to make the attribute-prop connection clear. 我选择了与awsFileKeys不同的属性名称,以使属性-属性连接清晰可见。 Of course, you can use the same name if you want: 当然,您可以根据需要使用相同的名称:

<DownloadReportsButton awsFileKeys={ this.state.awsFileKeys }/>

and therefore access the data as this.props.awsFileKeys in the DownloadReportsButton . 因此,可以在DownloadReportsButtonthis.props.awsFileKeys访问数据。

Hope it helps - Carlos 希望对您有所帮助-卡洛斯(Carlos)

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

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