Skip to content

The Input component implementations in the TodoMVC examples #1

@dmitriz

Description

@dmitriz

What is it about?

DISCLAIMER.

These are my notes, mostly for my own reference, to study and analyse the approaches by different frameworks to implement the popular TodoMVC example.

The comments record my own description of the approach for a more convenient future reference. No judgement and no neither positive nor negative connotation is neither implied
nor intended.

Redux (with React),

The TodoTextInput component from the TodoMVC example:

<input className={
    type="text"

    // controlled by the `text` state
    value={this.state.text}

    // emits 3 event actions
    onChange={this.handleChange}
    onKeyDown={this.handleSubmit} 
/>

State updates:

handleChange = e => this.setState({ text: e.target.value })

// needs to filter out only the ENTER events
// complex logic based on knowing about `newTodo`
// the `handleSubmit` handles the `onKeyDown` event
handleSubmit = e => {
    const text = e.target.value.trim()
    if (e.which === 13) {
      this.props.onSave(text)
      if (this.props.newTodo) {
        this.setState({ text: '' })
      }
    }
}

// update the state
handleChange = e => {
    this.setState({ text: e.target.value })
}

--
The AddTodo component from the Todos example

let input
<form onSubmit={e => {
  e.preventDefault()
  if (!input.value.trim()) {
    return
  }

  // action dispatching from the `input` variable
  dispatch(addTodo(input.value))
  
  // directly reset the value
  input.value = ''
}}>

  // directly referencing the node
  <input ref={node => {

    // set the `input` variable
    input = node
  }} />
  <button type="submit">
    Add Todo
  </button>
</form>

Un - related:

Basic submit example with un implemented with pure functions

The same example with the reset upon submit functionality with pure functions

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions