如何在材料ui popover中创建动态元素?

时间:2016-09-07 19:27:31

标签: reactjs material-ui

如何在material-ui popover中创建动态元素?我想要一个例子。

1 个答案:

答案 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或其他事件的提取,但此示例在装入组件时提取项目一次。