While becoming acquainted with React I stumbled upon the concept of portals in the developer documentation. However, I'm having difficulty understanding how this portal component actually renders on-demand, and how I can pass data to it to populate a modal.
Currently, I have two components that interact with each other: View.js
and DataTable.js
.
View.js
:
const Example = (props) => {
console.log(props);
return (
<div>
<TopBar />
<DeploymentsHeader env={props.match.params.env} />
<PendingDataTable env={props.match.params.env} />
<DataTable env={props.match.params.env} />
</div>
);
}
Now for the DataTable
component, a react-table is being rendered. When a user clicks on an individual row, my goal is to have a modal pop up (still unclear to me whether or not this needs to have its own separate component if I'm using React portals) and have it be populated with data that is already bound to the individual row (which I tested and have access too).
The code looks something like this:
<ReactTable
data={tableData}
filterable={true}
getTrProps={this.onRowClick}
columns={[
{
Header: "Header",
accessor: "service_name"
},
...
]}
/>
Now this is the function that gets passed to the table row props and executes on click:
onRowClick = (state, rowInfo) => {
return {
onClick: e => {
console.log('A Tr Element was clicked!');
console.log(rowInfo.original);
}
}
}
The data that I need is readily available to me in the object rowInfo.original
. Now my question is: what is considered the 'correct' or 'best-practice' way to load a modal using a portal when an event such as this onClick trigger executes?
- Do I need to have a separate
Modal.js
component that is actually a portal? - How do I get the data from this
onRowClick
function transported to this modal portal?
Thanks everyone.