[英]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.