Skip to main content

14.22 Combining Local Ui State And Redux

AddPerson.js

AddPerson.js
import React, { Component } from "react";
import "./AddPerson.css";

class AddPerson extends Component {
state = {
name: "",
age: "",
};

nameChangeHandler = (event) => {
this.setState({ name: event.target.value });
};

ageChangeHandler = (event) => {
this.setState({ age: event.target.value });
};
render() {
return (
<div className="AddPerson">
<input
type="text"
placeholder="Name"
onChange={this.nameChangeHandler}
value={this.state.name}
/>
<input
type="number"
placeholder="Age"
onChange={this.ageChangeHandler}
value={this.state.age}
/>
<button
onClick={() =>
this.props.personAdded(this.state.name, this.state.age)
}
>
Add Person
</button>
</div>
);
}
}

export default AddPerson;

Person.js

Person.js
import React, { Component } from "react";
import { connect } from "react-redux";
import AddPerson from "../components/AddPerson/AddPerson";
import Person from "../components/Person/Person";
import * as actionTypes from "../store/actions";

class Persons extends Component {
render() {
return (
<div>
<AddPerson personAdded={this.props.onAddedPerson} />
{this.props.prs.map((person) => (
<Person
key={person.id}
name={person.name}
age={person.age}
clicked={() => this.props.onRemovedPerson(person.id)}
/>
))}
</div>
);
}
}

const mapStateToProps = (state) => {
return {
prs: state.persons,
};
};

const mapDispatchToProps = (dispatch) => {
return {
onAddedPerson: (name, age) =>
dispatch({
type: actionTypes.ADD_PERSON,
personData: { name: name, age: age },
}),
onRemovedPerson: (id) =>
dispatch({ type: actionTypes.REMOVE_PERSON, personId: id }),
};
};

export default connect(mapStateToProps, mapDispatchToProps)(Persons);

reducer.js

reducer.js
import * as actionTypes from "./actions";

const initialState = {
persons: [],
};

const reducer = (state = initialState, action) => {
switch (action.type) {
case actionTypes.ADD_PERSON:
const newPerson = {
id: Math.random(),
name: action.personData.name,
age: action.personData.age,
};
return {
...state,
persons: state.persons.concat(newPerson),
};
case actionTypes.REMOVE_PERSON:
return {
...state,
persons: state.persons.filter(
(person) => person.id !== action.personId
),
};
default:
}
return state;
};

export default reducer;