大家好,我遇到了这个问题,但我在互联网上找不到解决方案,
我只想更改语言,并且当我在列表中选择语言时,但名称仍然停留在它上,因此我必须再次更改名称,以便更改名称,我正在使用react + redux和javaScript ,
我正在使用最后一个@ material-ui。
提前感谢:)
const options = [
'German',
'English',
];
class ListMenu extends React.Component {
button = null;
state = {
anchorEl: null,
selectedIndex: 1,
};
handleClickListItem = event => {
this.setState({ anchorEl: event.currentTarget });
};
handleMenuItemClick = (event, index) => {
this.setState({ selectedIndex: index, anchorEl: null });
var dispatch = this.props.dispatch;
if (index == 0){
dispatch(IntlActions.setLocale('de'));
}else if (index == 1) {
dispatch(IntlActions.setLocale('en'));
}
};
handleClose = () => {
this.setState({ anchorEl: null });
};
render() {
const { classes } = this.props;
const { anchorEl } = this.state;
return (
<div className={classes.root}>
<List component="nav">
<ListItem
button
aria-haspopup="true"
aria-controls="lock-menu"
aria-label="Language is"
onClick={this.handleClickListItem}
>
<ListItemText
primary="Language is"
secondary={options[this.state.selectedIndex]}
/>
</ListItem>
</List>
<Menu
id="lock-menu"
anchorEl={anchorEl}
open={Boolean(anchorEl)}
onClose={this.handleClose}
>
{options.map((option, index) => (
<MenuItem
key={option}
selected={index === this.state.selectedIndex}
onClick={event => this.handleMenuItemClick(event, index)}
>
{option}
</MenuItem>
))}
</Menu>
</div>
);