I have a problem of putting the parameter into the function. I've tried several solutions I've found here on Stack Overflow, but without success.
Here is my code:
function mapStateToProps(store) { return { un: store.measurement.unsaved, pat: store.patient.patients }; }
class MeasUnsaved extends Component{
constructor(){
super();
this.showBtn = this.showBtn.bind(this);
this.findPat = this.findPat.bind(this); this.createObj = this.createObj.bind(this);}
findPat(id){
let ps = this.props.pat.filter( p => (p.id === id) );
return ps[0];
}
createObj(){
let meas = [], i=0;
this.props.un.forEach( u => {
let pat = this.findPat(u.patId);
let k = { date: u.date, fnote: u.fnote, patId: u.patId, name: pat.lastName+' '+pat.name, pos: i };
i++;
meas.push(k);
});
return meas;
}
showBtn(val){
console.log(val);
}
render(){
const unsaved = this.createObj();
return (
<Well>
<fieldset>
<legend>Unsaved Measurement</legend>
<p>Unsaved Meas. will be lost after logout.</p>
{this.props.un.length === 0 && <p> You have 0 unsaved measurement </p>}
{this.props.un.length > 0 &&
<Table responsive>
<thead>
<tr><th>Date</th><th>Note</th><th>Patient</th><th>Actions</th></tr>
</thead>
<tbody>
{
unsaved.map(function(u){
return (
<tr key={u.date+u.patId.toString()}>
<td>{u.date}</td>
<td>{u.fnote}</td>
<td>{u.name}</td>
<td><Button bsSize="small" onClick={this.showBtn(u.pos)}>Show</Button></td>
</tr>
);
})
}
</tbody>
</Table>
}
</fieldset>
</Well>
);
}
} export default connect(mapStateToProps)(MeasUnsaved) ;
Here is the error:
ERROR: Uncaught Uncaught TypeError: Cannot read property 'showBtn' of undefined at onClick