最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Passing Multiple Arguments to Function in React - Stack Overflow

programmeradmin4浏览0评论

How do I pass multiple arguments to a function in React?

The below seems to make sense, but only data is retrieving the correct information. level is defined, but just an empty object.

<OutputContent data = { this.props.data } level = {0} />

function OutputContent({data}, level) {
    console.log(data);
    console.log(level);
    return <p>A simple example</p>
}

How do I pass multiple arguments to a function in React?

The below seems to make sense, but only data is retrieving the correct information. level is defined, but just an empty object.

<OutputContent data = { this.props.data } level = {0} />

function OutputContent({data}, level) {
    console.log(data);
    console.log(level);
    return <p>A simple example</p>
}
Share Improve this question edited Jul 8, 2018 at 11:57 Tholle 113k21 gold badges208 silver badges196 bronze badges asked Jul 8, 2018 at 11:56 Richard WhitehouseRichard Whitehouse 6913 gold badges14 silver badges28 bronze badges 1
  • For anyone having this question, you can learn all the details about deconstructing here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… – Mohsen Kamrani Commented Mar 12, 2020 at 0:07
Add a comment  | 

2 Answers 2

Reset to default 17

A stateless functional component like the one you have written gets all the props as the first argument.

function OutputContent(props) {
  console.log(props.data);
  console.log(props.level);
  return <p>A simple example</p>;
}

So if you want to use destructuring, make sure you desctructure all the props from the same object.

function OutputContent({data, level}) {
  console.log(data);
  console.log(level);
  return <p>A simple example</p>;
}

In addition, when your arguments is not only object but different types.

Function:

onChangeAddress = (e, flag) => {

     console.log(e.target.value);
     console.log(flag);
}

Caller

onChange={(e)=>this.onChangeAddress(e, 'r')}

Html Code

<Radio.Group onChange={(e)=>this.onChangeAddress(e, 'r')}>
    <Radio value={"Home"}>
        {language.switchValue?'Home Address':'বাসার ঠিকানা'}
    </Radio>
    <Radio value={"Office"}>
         {language.switchValue?'Office Address':'অফিসের ঠিকানা'}
    </Radio>
    <Radio value={"Other"}>
       {language.switchValue?'Other Address':'অন্যান্য ঠিকানা'}
    </Radio>
    <Radio value={"localPickup"}>
         {language.switchValue?'Pickup from Mall':'শপিং মল থেকে পিকআপ'}
    </Radio>
</Radio.Group>
发布评论

评论列表(0)

  1. 暂无评论