试图使用钩子将变量添加到状态

时间:2019-08-14 09:24:38

标签: react-native react-hooks

我无法将imageData添加到我的状态变量中

const fromLocalStorage = localStorage.getItem("test");

if (localStorage.getItem("test")) {
          const dataFromLocalStorage = JSON.parse(fromLocalStorage);
          let updatedData = data.map((d) => {

            const result = dataFromLocalStorage.find( dfls => dfls.id === d.id );
            return {...d, starIcon: result.starIcon} ;
          })
          this.setState({
            data: updatedData
          });
        } else {
          this.setState({ data: data.map(x => ({ ...x, starIcon: true })) });
        }

1 个答案:

答案 0 :(得分:1)

创建状态变量并将其添加为对象。


  const updateField = e => {
    setValues({
      ...autoData,
      [imageData]: e.value
    });
  };

  const [autoData, setValues] = useState({});