javascript - 带参数的 React(组件)onClick 方法

标签 javascript reactjs

我在将参数放入函数时遇到问题。我尝试了在 Stack Overflow 上找到的几种解决方案,但没有成功。

这是我的代码:

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) ;

这是错误:

ERROR: Uncaught Uncaught TypeError: Cannot read property 'showBtn' of undefined at onClick

最佳答案

你有两个问题;

  1. 您在“map”内使用“this” - 请参阅 "this" is undefined inside map function Reactjs

  2. 您在每一行上执行 this.showBtn,您想要的是将函数作为参数传递 - 这应该足够了:

    onClick={() => this.showBtn(u.pos)}

关于javascript - 带参数的 React(组件)onClick 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43786103/

相关文章:

javascript - 使用 javascript 运算符——对我来说是新的

javascript - 这个函数实际上是做什么的?

php - Wordpress:如果我想从 javascript 文件调用我的 php 脚本,该把它放在哪里?

reactjs - 如何使用 reactjs 提交父组件中的子值?

javascript - 弹出菜单呈现在与 anchor 元素不同的位置

javascript - Esprima 解析器工作示例

javascript - SwaggerUI 在向 API 发出请求时生成一个端口号。有什么办法可以阻止这种行为吗?

javascript - Bootstrap Form.Control 选择填充了格式为 'MONTH YYYY' 的日期字符串选项

javascript - React.js 如何使用虚拟 DOM 加速渲染

javascript - 在 reactJS 中调用 fetch 时需要修改 onClick 函数中的 URL