I have a list of items that are conditionally rendered onto my page- I'd like each of them to have a unique ref based on their ID.
I tried creating a ref for each item within the constructor, but it seems either because the refs are created before the render, or because of my own error- each ref is set to null.
class EmailList extends Component {
constructor(props) {
super(props)
this.state={
emailreports: []
}
props.emailreports.forEach(thing => {
this[`${thing.id}_ref`] = React.createRef()
});
}
componentDidMount(){
this.setState({
emailreports: this.props.emailreports
})
}
render() {
const { emailreports } = this.state;
return (
<div>
<section>
<h5>Email Reports</h5>
<ul>
{
emailreports.length > 0
?
<>
{
emailreports.map((report, i) => {
return <EmailReportItem ref={this[`${report.id}_ref`]} />
})
}
</>
:
<section >
<div className="text-center">You do not currently have access to email report data.</div>
</section>
}
</ul>
</section>
</div>
)
}
}
export default connect(mapStateToProps)(EmailList);
The following image is the result of the above load: