Fellows I have 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 an another Component that renders 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: can't define property "message": Object is not extensible
On the line that says:
this.props.message="Hello"
in method setMessage
of the AppButton
class.
I generated the react application using npm
and me 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" } }
For example, let's create a CustomButton class in React that takes color as a prop. To set a default value for the color prop, use the defaultProps property of CustomButton to set the default value of color to blue .
The defaultProps is a React component property that allows you to set default values for the props argument. If the prop property is passed, it will be changed. The defaultProps can be defined as a property on the component class itself, to set the default props for the class.
You can define default props this way: export class Counter extends React. Component { constructor(props) { super(props); this. state = {count: props.
You would change the prop in the parent component, as that is what holds the value of the prop itself. This would force a re-render of any child components that use the specific prop being changed. If you want to intercept the props as they're sent, you can use the lifecycle method componentWillReceiveProps .
I believe that defaultProps should do what you need:
import PropTypes from 'prop-types'; class AppButton extends Component { render(){ return ( <button onClick={this.props.onClick}>{this.props.message}</button> ) } }; AppButton.propTypes = { message: PropTypes.string, onClick: PropTypes.func }; AppButton.defaultProps = { message: 'Hello', onClick: function(){ alert("Hello"); } };
From the docs:
The defaultProps will be used to ensure that this.props.name will have a value if it was not specified by the parent component. The propTypes typechecking happens after defaultProps are resolved, so typechecking will also apply to the defaultProps.
Edit for clarity: There should be no need for you setMessage
in this instance.
return ( <button onClick={this.props.onClick}>{this.props.message || "Default text"}</button> );
This will check the value of prop and if it is undefined or null, the default message will replace the prop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With