[英]Adding a New Value to State is Overwriting Previous State in React Redux
My reducer adds a new workout object to an array called workouts.我的减速器将新的锻炼 object 添加到名为锻炼的数组中。 It works perfectly fine on the first initial change.它在第一次初始更改时工作得很好。 The problem is when I try to add another workout my reducer overwrites the previous workout I just added.问题是当我尝试添加另一个锻炼时,我的减速器会覆盖我刚刚添加的之前的锻炼。
I am pretty sure it has something to do with my reducer and how I'm manipulating the workouts array.我很确定这与我的减速器以及我如何操纵锻炼数组有关。
Form to add a new Workout:添加新锻炼的表格:
import React from 'react';
import { Field, reduxForm } from 'redux-form';
let Form = props => {
const { handleSubmit } = props;
return (
<form onSubmit={handleSubmit}>
<label>Name</label>
<Field name='name' component='input' type='text' />
<label>Total Calories</label>
<Field name='calories' component='input' type='number' />
<label>Duration</label>
<Field name='duration' component='input' type='number' />
<button type='submit'>Submit</button>
</form>
);
};
export default reduxForm({ form: 'workout' })(Form);
actions.js (my action): actions.js(我的操作):
export const addWorkout = workout => ({
type: 'ADD_WORKOUT',
payload: {
workout,
},
});
workouts.js (reducer):锻炼.js(减速器):
const initialState = {
workouts: [
{
name: 'HIIT',
calories: '320',
duration: '30',
},
{
name: 'Run',
calories: '35',
duration: '400',
},
],
totalCal: 0,
};
export default function workoutReducer(state = initialState, action) {
switch (action.type) {
case 'ADD_WORKOUT': {
console.log(state.workouts);
return {
...state,
workouts: [...state.workouts, action.payload.workout],
};
}
default:
return state;
}
}
Screenshots:截图:
Before adding any workouts, initial state contains two workouts, HIIT and Run在添加任何锻炼之前,初始 state 包含两个锻炼,HIIT 和跑步
After adding a workout, in this case I added a Yoga workout添加锻炼后,在这种情况下,我添加了瑜伽锻炼
Adding a second workout添加第二次锻炼
As you can see my first workout I added was overwritten.如您所见,我添加的第一次锻炼被覆盖了。
Any help would be very much appreciated, thanks in advance!任何帮助将不胜感激,在此先感谢!
Edit编辑
Here is my component that displays the list of workouts.这是我的显示锻炼列表的组件。
import React, { useEffect } from 'react';
import './Dashboard.scss';
import { connect } from 'react-redux';
import Chart from './Chart';
import Plan from './Plan';
const Dashboard = props => {
return (
<div className='dashboard-container'>
<div className='daily'>
<h4>Keep it going Steven you got this!</h4>
<div className='stats'>
<p className='calories'>Calories</p>
<p className='stand'>Stand Hours </p>
<p className='exercise'>Exercise Minutes</p>
</div>
<p>Workouts</p>
{props.workouts.map((workout, i) => {
return (
<div>
<p key={i}>{workout.name}</p>
</div>
);
})}
</div>
<div className='workouts'>
<Chart />
<Plan />
</div>
</div>
);
};
const mapStateToProps = state => ({
workouts: state.workouts.workouts,
totalCals: state.workouts.totalCal,
});
export default connect(mapStateToProps)(Dashboard);
The problem was regarding my Add Workout link in the Navbar.问题在于我在导航栏中的添加锻炼链接。 Instead of using Link from react-router-dom I was just using a Nav.Link tag from bootstrap which reset my state every time I navigated to the form.我没有使用 react-router-dom 中的链接,而是使用引导程序中的 Nav.Link 标签,每次导航到表单时都会重置我的 state。
import { Link } from 'react-router-dom';
...
<Nav className='mr-auto'>
...
{/* <Nav.Link href='/addworkout'>Add Workout</Nav.Link> */}
<Link to='/addworkout'>Add Workout</Link>
</Nav>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.