I'm new to ReactJS and I am currently trying to combine a RaisedButton and a Form Dialog from material-ui, however, I'm having trouble when trying to implement the onClick-function. Also a heads up: the code is very long. I know you are supposed to use a "small and verifiable example", however, I do not know which code is relevant and which isn't, since I'm so new to ReactJS.
Script for the button:
const styles = theme => ({
button: {
margin: theme.spacing.unit,
},
input: {
display: 'none',
},
});
function RaisedButtons(props) {
const { classes } = props;
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick = (e) => {
this.classes.handleClickOpen();
};
return (
<div>
<Button raised color="primary" className={classes.button} onClick={this.handleClick}>
Create a new file
</Button>
</div>
);
}
RaisedButtons.propTypes = {
classes: PropTypes.object.isRequired,
};
export default withStyles(styles)(RaisedButtons);
This is the script for the dialog(meaning the button in my dialog component, that calls the handleClickOpen-function, which does what its name implies).
<ButtonRaised handleClickOpen={this.handleClickOpen}/>
When trying to run the code, I get the following error (for my RaisedButton script):
Syntax error: Unexpected token, expected ; (19:21)
17 | const { classes } = props;
18 |
> 19 | constructor(props) {
| ^
20 | super(props);
21 | this.handleClick = this.handleClick.bind(this);
22 | }
I mean, I understand where the problem is, but I don't know how to fix it. I read several other questions, in which it said that the function probably isn't defined properly and that showed other ways to define them, but nothing helped. I tried removing the constructor method, however, then it told me that handleClick wasn't defined.
When writing the constructor method as
constructor = (props) => {
super(props);
this.handleClick = this.handleClick.bind(this);
}
, I got the following error:
Syntax error: 'super' outside of function or class (20:2)
Does anyone know what I'm doing wrong or how to fix that?