Guys, I made this component that creates a simple button:
class AppButton extends Component { setOnClick() { if(!this.props.onClick && typeof this.props.onClick == 'function') { this.props.onClick=function(){ alert("Hello"); } } } setMessage() { if(!this.props.message){ this.props.message="Hello" } } render(){ this.setOnClick() this.setMessage() return ( <button onClick={this.props.onClick}>{this.props.message}</button> ) } }
And I have another component that displays 2 buttons:
class App extends Component { render() { return ( <AppButton onClick={function(){ alert('Test Alert') } } message="My Button" /> <AppButton /> ); } }
But I get the following error:
TypeError: cannot define property "message": object is not extensible
On the line that says:
this.props.message="Hello"
in the setMessage method of the AppButton class.
Edit 1
I generated a response application using npm and my package.json has the following content
{ "name": "sample", "version": "0.1.0", "private": true, "dependencies": { "react": "^15.5.4", "react-dom": "^15.5.4" }, "devDependencies": { "react-scripts": "1.0.7" }, "scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test --env=jsdom", "eject": "react-scripts eject" } }
source share