【问题标题】:React-redux state is update, but i does not affect viewReact-redux 状态是更新,但我不影响视图
【发布时间】: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


    【解决方案1】:

    在这两种情况下,您都改变状态。

    首先,您复制对象,然后更改其中一个; React 看到相同的对象并认为没有任何变化。

    在第二个中,您创建一个新对象,但将其放在现有数组中。 React 仍然看到相同的数组并认为没有任何变化。

    您需要返回一个新数组,并为已更改的数组添加一个新对象。

    一种方法是基本上结合你的两次尝试:

    var stateClone = [...state];
    var cloneTask = {...state[action.index]};
    cloneTask.status = !cloneTask.status;
    stateClone[action.index] = cloneTask;
    return stateClone;
    

    应该可以。

    【讨论】:

    • 但是我记录了状态,我看到每次单击按钮时它都会更新。我将状态映射到道具,并且每次单击按钮时都会看到它更新。
    • 内容发生了变化,但 React 在首先查看引用的地方使用了优化。如果它仍然是同一个对象,则假定它没有改变。
    猜你喜欢
    • 2017-09-21
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2020-09-29
    • 1970-01-01
    • 2017-01-19
    • 2020-12-29
    相关资源
    最近更新 更多