次のケースの React SPA を作成しようとしています: School -Student1 -Student2 -Student3 -name -Address - 家番号、通り、都市、州、国。
考えてみると、School、Student、Address が反応コンポーネントです。School : 追加、削除可能な生徒のリストを保持/表示します。生徒: 名前、住所などの変更可能な情報を保持/表示します。住所: 変更可能な住所の詳細を保持/表示します。
さて、ここで状態管理を行うにはどうすればよいでしょうか。たとえば、ユーザーが生徒の 1 人の住所を変更した場合、これが学校にどのように反映されるのでしょうか。
私が考えることができるオプション: 1. Redux を使用しますが、この学校を別の反応アプリで使用されるコンポーネントとして作成したい場合、コンポーネント レベルで redux ストアを使用できますか?
- Student オブジェクトを Student 反応コンポーネントに渡します。このコンポーネントは、変更されたときにこれを情報で更新します..変化する小道具。
注:私は反応するのが初めてです。すみません、私が書いたことが間違っているか、意味がありません。
import * as React from 'react';
export default class School extends React.Component {
constructor(props) {
super(props);
this.state = {
schoolName: "test school",
students: [],
update:false
}
}
handleAddStudent = ()=>{
let student = {
name : "student1",
className: "class1",
address:{
houseNumber : "1-2/A-3",
street: "street1",
city: "city1",
country: "country1"
}
}
this.state.students.push(student);
this.setState({ update: true});
}
handleToJson = () => {
console.log(JSON.stringify(this.state));
}
render() {
return (
<div>
<ul>{
this.state.students.map((student)=>{
return <Student student={student}/>
})
}
</ul>
<button onClick={this.handleAddStudent}>Add Student </button>
<br/>
<button onClick={this.handleToJson}>To json</button>
</div>
);
}
}
export class Student extends React.Component {
constructor(props) {
super(props);
this.state = {
studentName: this.props.student.name,
className: this.props.student.className,
address: this.props.student.address
}
}
handleChange = (e)=>{
const value = e.target.value;
switch(e.target.name)
{
case 'studentName':
this.setState({ studentName: value});
this.props.student.name = value;
break;
case 'className':
this.setState({ className: value });
this.props.student.className = value;
break;
}
}
render() {
return (
<div>
<input name='studentName' value={this.state.studentName} onChange={this.handleChange} />
<input name='className' value={this.state.className} onChange={this.handleChange} />
<Address name='address' address={this.state.address}/>
</div>
);
}
}
export class Address extends React.Component {
constructor(props) {
super(props);
this.state = {
houseNumber: this.props.address.houseNumber,
street: this.props.address.street,
city: this.props.address.city,
country: this.props.address.country
}
}
handleChange = (e) => {
const value = e.target.value;
switch (e.target.name) {
case 'houseNumber':
this.setState({ houseNumber: value });
this.props.address.houseNumber = value;
break;
case 'street':
this.setState({ street: value });
this.props.address.street = value;
break;
case 'city':
this.setState({ city: value });
this.props.address.city = value;
break;
case 'country':
this.setState({ country: value });
this.props.address.country = value;
break;
}
}
render() {
return (
<div>
<input name='houseNumber' value={this.state.houseNumber} onChange={this.handleChange} />
<input name='street' value={this.state.street} onChange={this.handleChange} />
<input name='city' value={this.state.city} onChange={this.handleChange} />
<input name='country' value={this.state.country} onChange={this.handleChange} />
</div>
);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
ありがとう