Why does it have to be onChange, onClick, value

class DisplayMessages extends React.Component {
  constructor(props) {
    this.state = {
      input: '',
      messages: []
this.handleChange = this.handleChange.bind(this)
this.submitMessage = this.submitMessage.bind(this)
handleChange(event) {
    input: event.target.value
submitMessage() {
    messages: [...this.state.messages,this.state.input],

  render() {
    return (
        <h2>Type in a new Message:</h2>
        { /* render an input, button, and ul here */ }
<input onChange = {this.handleChange} value = {this.state.input}/>
<button onClick={this.submitMessage}>Submit</button>
{this.state.messages.map(key => {
  return <li key={key}>{key}</li>
        { /* change code above this line */ }

So for the props you pass to the input, button, why does it have to be called onChange, onClick? Is that mandatory?

If you’re asking why it’s onClick instead of a custom name, React defined element property names so it knows when to call the function you are passing to it. If you used anything else, React wouldn’t know when to call your function.

If you’re asking why it’s onClick instead of onclick, React decided to use camelCase to name their properties instead of the default HTML property names, because React is closer to JavaScript than it is to HTML.

1 Like

Thank you! Now I know.

1 Like