【发布时间】:2018-03-23 15:29:18
【问题描述】:
我正在尝试制作一个待办事项应用程序来学习 React Redux。 我的状态是这样的
[
{
taskName:"ABS",
status: true
},
{
taskName:"XYZ",
status: false
}
]
基本上,每个任务都是一个对象。我想在每次按下按钮时切换特定任务的状态(它会在红色和绿色之间改变颜色),所以我设计我的减速器如下:
尝试 1:
var stateClone = [...state];
stateClone[action.index].status = !stateClone[action.index].status;
return stateClone;
尝试 2:
var cloneTask = {...state[action.index]}
cloneTask.status = !cloneTask.status;
state[action.index] = cloneTask
localStorage.setItem('tasks', JSON.stringify(state));
return [...state];
据我了解,两者都是纯函数。两者都返回正确的状态,但是只有尝试 2 成功更新了视图,尝试 1 确实更新了状态但它没有正确更新视图。
谁来帮帮我。谢谢!
**
import React, { Component } from 'react';
import TaskItem from './TaskItem';
import { connect } from 'react-redux'
class TaskList extends Component {
constructor(props){
super(props);
this.state = {
filterName: '',
filterStatus: -1
};
}
onChange = (event)=>{
var target = event.target;
var name = target.name;
var value = target.value;
console.log(name)
this.props.onFilter(name==='filterName'?value:this.state.filterName,
name==='filterStatus'?value:this.state.filterStatus);
this.setState({
[name]:value
});
}
render() {
// console.log(this.props);
var { tasks } = this.props;
var { filterName, filterStatus } = this.state;
var elemTask = tasks.map((item, index)=>{
return <TaskItem
key={index+1}
item={item}
index={index}
deleteTask={this.props.deleteTask}
editTask = {this.props.editTask}
/>
});
return (
<table className="table table-bordered table-hover mt-15">
<thead>
<tr>
<th className="text-center">STT</th>
<th className="text-center">Task</th>
<th className="text-center">Status</th>
<th className="text-center">Action</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td>
<input
type="text"
className="form-control"
name="filterName"
value={filterName}
onChange={this.onChange}
/>
</td>
<td>
<select name="filterStatus"
value={filterStatus}
className="form-control"
onChange={this.onChange}>
<option value={-1}>All</option>
<option value={0}>OnHold</option>
<option value={1}>Active</option>
</select>
</td>
<td></td>
</tr>
{elemTask}
</tbody>
</table>
);
}
}
const mapStateToProps = (state) => {
return {
tasks: state.tasks
};
}
export default connect(mapStateToProps, null)(TaskList);
**
我的 react 文件应该从 store 中获取状态
import * as types from '../constants/ActionTypes';
var data = JSON.parse(localStorage.getItem('tasks'));
var initialState = data? data: [];
var myReducer = (state=initialState, action) => {
switch(action.type){
case types.LIST_ALL:
return state;
case types.ADD_TASK:
var newTask = {
id: Math.random(),
name: action.task.name ,
status: action.task.status==='true'
}
state.push(newTask);
localStorage.setItem('tasks', JSON.stringify(state));
return [...state];
case types.UPDATE_STATUS:
// var stateClone = [...state];
// stateClone[action.index].status = !stateClone[action.index].status;
// console.log(stateClone);
// return stateClone;
var cloneTask = {...state[action.index]}
cloneTask.status = !cloneTask.status;
state[action.index] = cloneTask
localStorage.setItem('tasks', JSON.stringify(state));
console.log(state);
return [...state];
default: return state
}
};
export default myReducer;
【问题讨论】:
标签: javascript reactjs redux