简体   繁体   English

如何在React中的render方法之外的函数中渲染组件?

[英]How to render component from a function outside the render method in React?

I have a React class in which Im using a plugin called React CSV which converts an array of values into CSV and starts a download of the newly created CSV file in the browser. 我有一个React类,其中Im使用一个名为React CSV的插件,该插件将值数组转换为CSV并开始在浏览器中下载新创建的CSV文件。

I call a function when the button called Export is clicked. 单击名为“导出”的按钮时,我会调用一个函数。 This function is outside the render function(Same React class though). 该函数在render函数之外(尽管与React类相同)。 The component triggers a download of the CSV file as soon as its rendered. 呈现后,该组件会立即触发CSV文件的下载。

My button component, inside the render() method : 我的按钮组件,位于render()方法内部:

    <Button 
        variant="contained" 
        color="secondary"
        onClick={this.exportCSV.bind(this)}
    >
      Export
    </Button>

My exportCSV function which is defined before the render method is as follows: 我的exportCSV函数在render方法之前定义如下:

      exportCSV(){
    const csvMergedData = this.props.dataA.map((value, index) 
      => ({
            columnA: value,
            columnB: this.props.dataB[index]
    }));
   return (<CSVDownload data={csvMergedData} /> );

} }

The problem is, CSVDownload component is not getting mounted/rendered, hence the download of the CSV file isn't happening. 问题是,CSVDownload组件未安装/渲染,因此没有下载CSV文件。

How to render the component? 如何渲染组件?

PS : I have gone through other SO answers, but I couldn't find the solution. PS:我已经解决了其他问题,但是找不到解决方案。

The CSVDownload component isn't mounting because returning the component from an event handler function does not render the component. CSVDownload组件未安装,因为从事件处理程序函数返回该组件不会呈现该组件。 In order to render a component, it needs to be in the render method. 为了渲染组件,它必须在render方法中。

I would suggest putting csvMergedData into your component's state and initializing it to null: 我建议将csvMergedData放入组件的状态并将其初始化为null:

class YourComponent extends React.Component {
    state = { csvMergedData: null };

Then in your exportCSV function, you can do the mapping and save it in state: 然后,在exportCSV函数中,可以进行映射并将其保存为状态:

exportCSV() {
    const csvMergedData = this.props.dataA.map((value, index) => ({
        columnA: value,
        columnB: this.props.dataB[index]
    }));
    this.setState({ csvMergedData });
}

And finally in your render method, you can conditionally render the CSVDownload component depending on the state: 最后,在您的render方法中,您可以根据状态有条件地渲染CSVDownload组件:

<Button 
    variant="contained" 
    color="secondary"
    onClick={this.exportCSV.bind(this)}
>
  Export
</Button>

{this.state.csvMergedData
    ? <CSVDownload data={this.state.csvMergedData} />
    : null
}

Indeed you can't render react component this way. 实际上,您不能以这种方式渲染react组件。 A fix for you would be 一个适合您的解决方案是

class SomeComponent extends React.Component {
  state = {
    data: false
  }

  exportCsv = () => {
    const data = this.props.dataA.map((value, index)
    => ({
          columnA: value,
          columnB: this.props.dataB[index]
    }));
    this.setState({ data })
  }

  render() {
    return <React.Fragment>
      <Button 
        variant="contained" 
        color="secondary"
        onClick={this.exportCsv}
      >
        Export
      </Button>
      { this.state.data ? <CSVDownload data={this.state.data} /> : null}
    </React.Fragment>
  }
}

暂无
暂无

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

相关问题 如何从渲染函数外部引用React Component类? - How to reference a React Component class from outside the render function? 使用外部 function 有条件地渲染组件 - React conditionally render component using an outside function 调用外部函数时如何重新渲染 React 组件? - How to re-render React Component when outside function is called? 从render方法中的嵌套函数中反映调用组件的方法 - React call component's method from nested function in render method 如何将 function 从 FUNCTIONAL 传递给 CLASS 组件并在渲染外部访问它(没有 prop ),在反应 js 中使用上下文? - How to pass function from FUNCTIONAL to CLASS component and access it outside of render( without prop ), using context in react js? React-如何在render之外的函数中渲染组件以及如何在render之外执行相同的函数? - React - How to render components inside a function outside of render plus execute same function outside of render? 如何从另一个组件调用React的render方法()? - How to call React's render method() from another component? 如何在包含渲染第一个组件的函数的组件之外渲染组件? - How to render a component outside the component that contains the function that renders the first component? 反应:从渲染方法外部更新组件的道具而不使用状态 - React: update component's props from outside of render method without using state 在 React 中,如何测试组件内的渲染方法 - In React, how to test a render method inside a component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM