简体   繁体   English

保存时无法保持复选框选中

[英]Unable to keep checkbox checked on save

Issue: Every time I refresh the page, the previously checked boxes get unchecked.问题:每次我刷新页面时,以前选中的框都会取消选中。 I have 4 checkboxes.我有 4 个复选框。 I checked two and then I refresh.我检查了两个,然后刷新。 The other two wont save.其他两个不会保存。 Im not sure what's wrong here.我不确定这里有什么问题。 Could you please point my mistake.你能指出我的错误吗? Much Appreciated.非常感激。

const handleCheckboxChange = ([event]) => {
    return event.target.checked;
  };

  const organizationInfo = {
    // label name, variable name, value
    "Organization Name": ["name", name],
    "Organization Contact E-mail": ["email", email],
  };

 const renderNeedSection = () => {
    if (organization) {
      return (
        <div>
          {Object.entries(NEEDS).map(([key, label]) => (
            <CheckBoxWrapper key={key}>
              <Controller
                as={Checkbox}
                defaultChecked={needs[key]}
                name={`needs.${key}`}
                control={control}
                onChange={handleCheckboxChange}
              >
                <Label inputColor="#000000">{label}</Label>
              </Controller>
            </CheckBoxWrapper>
          ))}
          <span style={errorStyles}>
            {errors.needs ? "Please select at least one option" : ""}
          </span>
        </div>
      );
    }
  };

React state isn't persisted across reloads. React state 不会在重新加载时保持不变。 If you want your data to persist you have among other options如果您希望您的数据持续存在,您还有其他选择

  1. Store it in a server and retrieve it when the page loads将其存储在服务器中并在页面加载时检索它
  2. Use the browser localStorage使用浏览器localStorage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM