【问题标题】:How to add multiple parameters in arrow function [duplicate]如何在箭头函数中添加多个参数[重复]
【发布时间】:2021-06-09 21:13:39
【问题描述】:

已经为我的愚蠢问题感到抱歉,但谷歌搜索没有成功

如何在箭头函数中添加多个参数。我想在下面的函数中添加一些属性“props”。

  const changeLocationHandler = async event => {
    try {
      let hasError = false;
      let response = await fetch('http://localhost:5000/api/game/location', {
        method: 'POST',
        headers: {
          Authorization: 'Bearer ' + auth.token
        },
        body: { 
        }
      });
      const responseData = await response.json();
      if (!response.ok) {
        hasError = true;
      }
      if (hasError) {
        alert(responseData.message);
      }  
    } catch (error) {
      alert(error)
    }
  }

它不接受类似的东西

const changeLocationHandler = async event props => {

const changeLocationHandler = props => async event => {

提前致谢

【问题讨论】:

  • (event, props) => { ... } javascript.info/arrow-functions-basics
  • 只有1个参数的函数可以缺少()
  • 这样的问题通常由谷歌搜索回答,如“mdn 箭头函数”
  • 对不起,问这个问题的人 - 正如我所说的那样,这将是一个愚蠢的问题,是的,我看到了 mdn 网页但不明白它。无论如何感谢您的回答,它现在可以工作了。

标签: javascript reactjs arrow-functions


【解决方案1】:

您需要将参数括在括号中才能正常工作。

const changeLocationHandler = async (event, arg2, arg3) => {

【讨论】:

    猜你喜欢
    • 2023-03-03
    • 2020-07-03
    • 2015-05-02
    • 2016-03-25
    • 2017-10-02
    • 2021-04-22
    • 2021-03-29
    • 2020-11-30
    相关资源
    最近更新 更多