作成します https://github.com/storenth/react-redux-todo そして、プログラミングパターンについて私のTodo-react-appを確認するように依頼します。コンポーネントをプレゼンテーションコンポーネントとコンテナコンポーネントに分割します。
その背後にある基本的な考え方は次のとおりです。コンポーネントに状態が必要な場合、小道具に基づいて計算を行う場合、またはその他の複雑なロジックを管理する必要がある場合、そのコンポーネントはHTMLのようなJSXをレンダリングする必要もありません。
HTMLのようなJSXをレンダリングする代わりに、コンポーネントは別のコンポーネントをレンダリングする必要があります。HTMLのようなJSXをレンダリングするのはそのコンポーネントの仕事であるはずです。
だから、/containers/TodosContainerそれについての私の質問にはとが含まれ/components/Todosてい/components/AddFormます。パターンと一致していますか?どこに置くか私が取得できませんでしたAddForm、内部/components/Todos(複合体Iの推測にしますか)?
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} />
</>
)
}
}
```