I create https://github.com/storenth/react-redux-todo and ask to review my Todo-react-app for programming pattern: dividing components into presentational components and container components.
Here’s the basic idea behind it: if a component has to have state, make calculations based on props, or manage any other complex logic, then that component shouldn’t also have to render HTML-like JSX.
Instead of rendering HTML-like JSX, the component should render another component. It should be that component’s job to render HTML-like JSX.
So, my question about the /containers/TodosContainer
that contains /components/Todos
and /components/AddForm
. Does it match the pattern? I didn't get where to place the AddForm
, inside /components/Todos
(to complex I guess)?
import React, { Component, Fragment } from 'react';
import { Todos } from "../components/Todos";
import { AddForm } from "../components/AddForm";
export class TodosContainer extends Component {
constructor(props) {
super(props);
this.state = { items: [], text: '' };
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.removeItem = this.removeItem.bind(this);
}
handleChange = (e) => {
this.setState({
text: e.target.value
})
}
handleSubmit = (e) => {
e.preventDefault();
if (this.state.text.length === 0) {
return;
}
const newItem = {
text: this.state.text,
id: Date.now()
};
this.setState({
items: this.state.items.concat(newItem),
text: ''
})
}
removeItem = (id) => {
const items = this.state.items.filter(item => {
return item.id !== id;
})
this.setState({
items
})
console.log(this.state);
}
render() {
return (
<>
<Todos items={this.state.items} removeItem={this.removeItem} />
<AddForm items={this.state} onSubmit={this.handleSubmit} onChange={this.handleChange} />
</>
)
}
}
```