The easiest solution (though not the most performant) is assigning a wrapped function to the event listener.
{this.state.sb_headers.map((header, index) => (
<Link key={index} to={header.title} className="list-group-item list-group-item-action" aria-
current="true" onClick={() => this.storeUrl(header)}>
<span className='icon-text'>{header.title}</span>
</Link>
))}
You can see I created a fat arrow function that invoked storeUrl with header as the parameter. This creates a new function for every Link on every render. Not optimal but it'll do the trick. Another way to write this is as a function that returns a function:
function storeUrl(header) {
return function(e) {
// This is the function that's called on click, it has access
// to header via lexical scope
}
}
{this.state.sb_headers.map((header, index) => (
<Link key={index} to={header.title} className="list-group-item list-group-item-action" aria-
current="true" onClick={this.storeUrl(header)}>
<span className='icon-text'>{header.title}</span>
</Link>
))}
The final option is to attach some data to the button that can be retrieved through the click event.
{this.state.sb_headers.map((header, index) => (
<Link key={index} to={header.title} className="list-group-item list-group-item-action" aria-
current="true" onClick={this.storeUrl} data-index={index}>
<span className='icon-text'>{header.title}</span>
</Link>
))}
// Your storeUrl method will receive e as an event
function storeUrl(e) {
// Get more information about the click
// The value will be a string so the + transforms it back to a number
let index = +e.target.getAttribute('data-index');
let header = this.state.sb_headers[index];
// Whatever you were going to do with header
}
This solution is more performant as it doesn't create a bunch of extra wrapped functions, though the performance is can be negligible depending on the application size.
this.storeUrl(url). Just pass the URLs to yourthis.storeUrlfunction.onClick={(url)=>this.storeUrl(url)}, once u do this, within the storeUrl function u can store the url in localStorage.