useState Reactjs не обновляется - PullRequest
       0

useState Reactjs не обновляется

0 голосов
/ 23 сентября 2019

Я пытаюсь использовать React Hooks, но каким-то образом мое состояние не обновляется

  const [Choices, setChoices] = useState([]);

  useEffect(() => {
    async function getUsers() {
      // Here’s the magic
      let tempChoices = [];
      const promises = MAINLIST.List.ChoicesFields.map(async z => {
        tempChoices[z] = [];

        let GetChoicesFromInternalFieldResults = await GetChoicesFromInternalField(
          z
        );
        GetChoicesFromInternalFieldResults.map(c => {
          tempChoices[z].push({
            key: c,
            text: c,
            value: c
          });
        });
      });
      await Promise.all(promises);
      const object = { Choices: tempChoices };
      // THIS IS PRINTING CORRECT VALUES
      console.log(object);
      setChoices(object);
    }
    getUsers();
  }, []);

Вот результат console.log

enter image description here

но когда я проверяю состояние в инструменте разработчика, состояние пусто

enter image description here

1 Ответ

0 голосов
/ 23 сентября 2019

Я нашел свои ответы.Я превратил «tempChoices» в массив вместо объекта.

  const [Choices, setChoices] = useState({});

  useEffect(() => {
    async function getUsers() {
      // Here’s the magic
      let tempChoices = {};
      const promises = MAINLIST.List.ChoicesFields.map(async z => {
        tempChoices[z] = [];

        let GetChoicesFromInternalFieldResults = await GetChoicesFromInternalField(
          z
        );
        GetChoicesFromInternalFieldResults.map(c => {
          tempChoices[z].push({
            key: c,
            text: c,
            value: c
          });
        });
      });
      await Promise.all(promises);
      const object = { Choices: tempChoices };

      setChoices(object);
    }
    getUsers();
  }, []);
...