简体   繁体   English

expo | 调用箭头函数时仅更改 state?

[英]expo | only changes on state while call arrow-function?

i have a problem with following function;我在关注 function 时遇到问题;

const handleSave = (task, description, priorityState, taskID) => {
  changeLabel(task, taskID);
  changeDescription(description, taskID);
  changePriority(priorityState, taskID);
  navigation.goBack();
};

the problem is thats only change the last used function:问题是那只是改变最后使用的 function:

if i'll change the Label and the Description its only save the Description for example.例如,如果我要更改 Label 和描述,它只会保存描述。 < i call the function while clicking a Button (TouchableOpacity) < 我在单击按钮 (TouchableOpacity) 时调用 function

<Button
  title={language.edit.save}
  color={theme.secondary}
  onPress={() => {
    handleSave(task, description, priorityState, taskID);
  }}
/>

any advise?有什么建议吗?

what did i try?我尝试了什么? delay the functions:延迟功能:

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

const handleSave = (task, description, priorityState, taskID) => {
  changeLabel(task, taskID);
  sleep(10); 
  changeDescription(description, taskID);
  sleep(10); ...
};

i would like to thank any kind of help <3我想感谢任何形式的帮助 <3

full code here.完整代码在这里。

Your 3 custom functions changeLabel , changeDescription and changePriority all try to update the same tasks state:您的 3 个自定义函数changeLabelchangeDescriptionchangePriority都尝试更新相同tasks state:

Repo source 回购来源

const [tasks, setTasks] = useState([]);

const changePriority = (color, taskID) => {
  const task = tasks.map(/* etc. */);
  setTasks(task);
};

const changeDescription = (description, taskID) => {
  const task = tasks.map(/* etc. */);
  setTasks(task);
};

const changeLabel = (value, taskID) => {
  const task = tasks.map((/* etc. */);
  setTasks(task);
};

When you successively call these functions, each one reads the current tasks state, which has not been updated yet by the previous call to setTasks SetStateAction.当您连续调用这些函数时,每个函数都会读取当前tasks state,该任务尚未被之前调用setTasks SetStateAction 更新。 See also The useState set method is not reflecting a change immediately另请参阅useState set 方法未立即反映更改

You can easily solve this by using the functional update form of the SetStateAction:您可以使用 SetStateAction 的功能更新形式轻松解决此问题:

If the new state is computed using the previous state, you can pass a function to setState .如果新的 state 是使用之前的 state 计算的,您可以将 function 传递给setState The function will receive the previous value, and return an updated value. function 将接收先前的值,并返回更新后的值。

In your case, change your 3 functions to get the previous state as a parameter:在您的情况下,更改您的 3 个函数以获取先前的 state 作为参数:

const changePriority = (color, taskID) => {
  setTasks(
    // Use previous tasks received as argument
    // instead of directly tasks which may be
    // an old value.
    (previousTasks) => previousTasks.map(/* etc. */)
  );
};

// Same for the other 2 functions.

The solution from @ghybs worked. @ghybs 的解决方案有效。 I appreciate your work Thx我很欣赏你的工作谢谢

i changed the function from:我将 function 从:

const markTask = (taskID) => {
  const task = tasks.map((item) => {
    if (item.id == taskID) {
      return { ...item, completed: true };
    }
    return item;
  });
  setTasks(task);
};

to this:对此:

const changeLabel = (value, taskID) => {
  setTasks((previousTasks) =>
    previousTasks.map((item) => {
      if (item.id == taskID) {
        return { ...item, label: value };
      }
      return item;
    })
  );
};

snack with the issue有问题的零食

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

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