如何获得参考数组

时间:2019-01-11 00:40:32

标签: reactjs refs

大家好!

export default class Test extends React.Component {
    constructor(props) {
        super(props);
        this.ref = [];
    }

    handleClick = () => {
        let doc;
        doc = this.refs["link" + 0];
        console.log("ref is => " + doc.innerHTML);
    }

    render() {
        return (
            <div>
                <p ref={"link" + 0} onClick={this.handleClick}> p1</p>
                <p ref={"link" + 1} onClick={this.handleClick}> p2</p>
            </div>
        );
    }
}

当我输入handleClick函数时,如何知道我单击了哪个链接? 感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

  export default class Test extends React.Component {

    handleClick = (linkRef) => {
        console.log("ref is => " + this.refs[linkRef].innerHTML);
    }

    render() {
        return (
            <div>
                <p ref='linkOne' onClick={() => this.handleClick('linkOne')}> p1</p>
                <p ref='linkTwo' onClick={() => this.handleClick('linkTwo')}> p2</p>
            </div>
        );
    }
}