如何在material-ui popover中创建动态元素?我想要一个例子。
答案 0 :(得分:1)
class MyPopover extends React.Component {
constructor(props) {
super(props);
this.state = { popoverItems: [] };
}
componentDidMount() {
... fetch popover items ...
... set state ...
}
render() {
return (
<MuiThemeProvider>
<Popover
open={this.state.open}
anchorEl={this.state.anchorEl}
anchorOrigin={{horizontal: 'left', vertical: 'bottom'}}
targetOrigin={{horizontal: 'left', vertical: 'top'}}
onRequestClose={this.handleRequestClose}
>
<Menu>
{this.state.popoverItems.map( (item) => {
<MenuItem primaryText={item.name} />
})}
</Menu>
</Popover>
</MuiThemeProvider>
)
}
}
您也可以执行onClick或其他事件的提取,但此示例在装入组件时提取项目一次。