【问题标题】:checkbox does not retain state in React复选框在 React 中不保留状态
【发布时间】:2016-10-13 09:27:44
【问题描述】:

当我单击标签面板中的复选框时,它会按预期被选中。但是当我切换到另一个面板,然后再切换回来时,我发现应该选中的复选框变成了未选中。我该如何解决这个问题?

这是我的代码,我使用来自 npm 的 react-tab-panel

Container:

export default class Container extends Component {
    static propTypes = {
    };
    constructor(props) {
        super(props);
        this.state = {
            chosenItem: {
                a: [],
                b: [],
                c: [],
                d: []
            }
        };
    }

    handleCheckBoxChange = (e) => {
        const value = e.target.value;
        const item = e.target.attributes.getNamedItem('data-tag').value;
        const chosenItem = this.state.chosenItem;
        const index = chosenItem[item].indexOf(value);
        if (index > -1) {
            chosenItem[item].splice(index, 1);

        }
        chosenItem[item].push(value);

    }

    render() {
        return (
            <div>
                <Panel
                    handleCheckBoxChange={this.handleCheckBoxChange}

                />
            </div>
        );
    }
}

Panel:

import TabPanel from 'react-tab-panel';
import 'react-tab-panel/index.css';
import CheckBox from './CheckBox.jsx';

export default function Panel({

    handleCheckBoxChange
}) {
    const tabList = {
        a: ['1', '2'],
        b: ['1', '2'],
        c: ['1', '2'],
        d: ['1', '2']
    };

    return (
        <TabPanel
            tabPosition="left"
        >
        {
            Object.keys(tabList).map((key) => {
                return (
                    <form
                        key={key}
                        tabTitle={key}
                    >
                    {
                        tabList[key].map((item) =>
                            <CheckBox
                                key={item}
                                value={item}
                                handleCheckBoxChange={handleCheckBoxChange}
                                tag={key}
                            />
                        )
                    }
                    </form>
                );
            })
            }
        }
        </TabPanel>
    );
}

Checkbox:

export default function CheckBox({
    tag,
    value,

    handleCheckBoxChange
}) {
    return (
        <div>
            <label>
                <input
                    type="checkbox"
                    value={value}

                    onChange={handleCheckBoxChange}
                    data-tag={tag}
                />
                {value}
            </label>
        </div>
    );
}

【问题讨论】:

  • 您应该使用setState 来更改组件的状态。
  • @robertklep setState 在我的情况下不是一个好的解决方案。因为我也使用 react-router,所以如果我改变路径然后回到这个路径,所有的状态都会消失。

标签: forms checkbox input reactjs


【解决方案1】:

您应该考虑使用FluxRedux 来存储您的应用程序状态树。

【讨论】:

  • 我只是在使用 redux。我已经用 redux 修复了它。无论如何,谢谢。
  • @Downpooooour 你能分享一下你是如何解决这个问题的吗?
猜你喜欢
  • 1970-01-01
  • 2022-01-17
  • 2020-06-11
  • 2017-02-27
  • 1970-01-01
  • 1970-01-01
  • 2023-02-16
  • 2016-01-01
  • 2019-08-10
相关资源
最近更新 更多