Skip to content

Instantly share code, notes, and snippets.

@antonmedv
Created May 19, 2017 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antonmedv/d829b7c6602dc8d8656eb7f9aa0f135e to your computer and use it in GitHub Desktop.
Save antonmedv/d829b7c6602dc8d8656eb7f9aa0f135e to your computer and use it in GitHub Desktop.
jsx vs mx
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import TodoItem from './TodoItem'
import Footer from './Footer'
import { SHOW_ALL, SHOW_COMPLETED, SHOW_ACTIVE } from '../constants/TodoFilters'
const TODO_FILTERS = {
[SHOW_ALL]: () => true,
[SHOW_ACTIVE]: todo => !todo.completed,
[SHOW_COMPLETED]: todo => todo.completed
}
export default class MainSection extends Component {
static propTypes = {
todos: PropTypes.array.isRequired,
actions: PropTypes.object.isRequired
}
state = { filter: SHOW_ALL }
handleClearCompleted = () => {
this.props.actions.clearCompleted()
}
handleShow = filter => {
this.setState({ filter })
}
renderToggleAll(completedCount) {
const { todos, actions } = this.props
if (todos.length > 0) {
return (
<input className="toggle-all"
type="checkbox"
checked={completedCount === todos.length}
onChange={actions.completeAll} />
)
}
}
renderFooter(completedCount) {
const { todos } = this.props
const { filter } = this.state
const activeCount = todos.length - completedCount
if (todos.length) {
return (
<Footer completedCount={completedCount}
activeCount={activeCount}
filter={filter}
onClearCompleted={this.handleClearCompleted}
onShow={this.handleShow} />
)
}
}
render() {
const { todos, actions } = this.props
const { filter } = this.state
const filteredTodos = todos.filter(TODO_FILTERS[filter])
const completedCount = todos.reduce((count, todo) =>
todo.completed ? count + 1 : count,
0
)
return (
<section className="main">
{this.renderToggleAll(completedCount)}
<ul className="todo-list">
{filteredTodos.map(todo =>
<TodoItem key={todo.id} todo={todo} {...actions} />
)}
</ul>
{this.renderFooter(completedCount)}
</section>
)
}
}
import TodoItem from './TodoItem'
import Footer from './Footer'
let filteredTodos = todos.filter(TODO_FILTERS[filter])
let completedCount = todos.reduce((count, todo) => todo.completed ? count + 1 : count, 0)
section className="main"
if todos.length > 0
input(
className="toggle-all"
type="checkbox"
checked={completedCount === todos.length}
onChange={actions.completeAll}
)
ul className="main"
for todo of filteredTodos
TodoItem key={todo.id} todo={todo} {...actions}
if todos.length
Footer(
completedCount={completedCount}
activeCount={activeCount}
filter={filter}
onClearCompleted={actions.clearCompleted}
onShow={handleShow}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment