I am very new to React and so I wonder sometimes when I got something working if I did it the "correct" way.
I wrote a simple TimePickerComponent(based on material-ui-next):
class TimePickerComponent extends React.Component {
_placeholder;
_defaultTimeString;
_timeString;
_errorStatus;
_classes;
constructor({ placeholder, defaultTimeString, timeString, errorStatus, classes }) {
super();
this._placeholder = placeholder;
this._defaultTimeString = defaultTimeString;
this._timeString = timeString;
this._errorStatus = errorStatus;
this._classes = classes;
}
get Placeholder() {
return this._placeholder;
}
get DefaultTimeString() {
return this._defaultTimeString ? this._defaultTimeString : CONTROLS_CONSTANTS.DEFAULT_TIME_STRING;
}
get TimeString() {
return this._timeString;
}
get ErrorStatus() {
return this._errorStatus;
}
get Classes() {
return this._classes;
}
render() {
return <FormControl>
<TextField error={this.ErrorStatus}
label={this.Placeholder}
defaultValue={this.TimeString ? this.TimeString : this.DefaultTimeString}
className={this.Classes.layout}
type="time"
InputLabelProps={{
shrink: true
}}
/>
</FormControl>
}
}
TimePickerComponent.propTypes = {
placeholder: PropTypes.string.isRequired,
defaultTimeString: PropTypes.string,
timeString: PropTypes.string,
errorStatus: PropTypes.bool
}
export default withStyles(styles)(TimePickerComponent);
As you see I created "private" fields (I know that they are not private in this case) and in the constructor, I assign the values I got as props to these fields.
And the getters just return the private fields or include some logic like the DefaultTimeString getter.
My question now: Is this the or a correct way? Or will I have problems with this? For me, it is very straightforward and self-explaining.
Thank you in advance.
componentWillMounthook.const TimePickerComponent = ({ placeholder, defaultTimeString, timeString, errorStatus, classes }) => /* JSX from render() */. But this assumes that the component is only used to present information in a certain way and not perform any logic.