Redux不立即更新状态

时间:2020-03-29 12:21:39

标签: javascript reactjs redux

我正在从事配方设计师的工作。 我通过Api获取数据,并通过Picker获取用户名

 const [selectedFriend, setSelectedFriend] = useState("");

const data = useCallback(() => {
const jobData = {
  description: props.Plot,
  titel: props.Title,
  year: props.Year,
  friend: selectedFriend,
};

dispatch(jobActions.postJob(jobData));
}, [description, titel, friend, ]);

      <Picker
        style={{ flex: 1, color: "white", textAlign: "center" }}
        selectedValue={selectedFriend}
        onValueChange={newFriendName => setSelectedFriend(newFriendName)}
      >
        {allFriendlist.map((item, index) => {
          return (
            <Picker.Item
              label={item.id}
              value={item.id}
              key={index}
              style={{ color: "white", textAlign: "center" }}
            />
          );
        })}
      </Picker>

成功提交后,我会收到警报。当按“是”时,应将我的selectedFriend值设置为一个空字符串。但是看来我的商店“落后了”。如果我从列表中选择自己的名字并尝试发送表格,则不会发生任何事情。如果我更改类似“;”的内容在我的代码中,并重新加载它以加载我以前的状态。 这是我第三天试图解决这个难题。

  useEffect(() => {
    if (jobCreated) {
      Alert.alert(".", "", [
        {
          text: "yes",
          onPress: () => {
            setDescription("");
            setTitel("");
            setfName("");
            setSelectedFriend("")

            console.log(selectedFriend);
          }
        }
      ]);
    }
  }, [jobCreated]);

非常感谢!如果需要进一步的信息,我会提供。

谢谢!

0 个答案:

没有答案