ElemeFE / element-react

Element UI
https://elemefe.github.io/element-react/index
MIT License
2.83k stars 443 forks source link

How to use onClick() at <Menu.Item> to run method? #957

Closed v2okimochi closed 5 years ago

v2okimochi commented 5 years ago
clicked() {
    console.log('clicked')
}
render() {
    return (
        <div>
            <Menu theme="dark" className="el-menu-demo" mode="horizontal">
                <Menu.Item index="1" onClick={() => this.clicked()}>item1</Menu.Item>
                <Menu.Item index="1">item2</Menu.Item>
                <Menu.Item index="1">item3</Menu.Item>
            </Menu>
        </div>
    )
}

In these snipets, I'm expected that clicked() will be called if I click the Menu-Item "item1". Thanks.

loadchange commented 5 years ago
clicked(idx) {
    console.log(`select: ${idx}`)
}
render() {
    return (
        <div>
            <Menu 
                theme="dark" 
                className="el-menu-demo" 
                mode="horizontal" 
                onSelect={idx => this.clicked(idx)}
            >
                <Menu.Item index="1">item1</Menu.Item>
                <Menu.Item index="2">item2</Menu.Item>
                <Menu.Item index="3">item3</Menu.Item>
            </Menu>
        </div>
    )
}