Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Multiple Arguments to Function in React

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>
}
like image 637
Richard Whitehouse Avatar asked Jul 08 '18 11:07

Richard Whitehouse


People also ask

How do you pass multiple parameters to a function in react?

To pass multiple parameters to onChange in React:Pass an arrow function to the onChange prop. The arrow function will get called with the event object. Call your handleChange function and pass it the event and the rest of the parameters.

Why is && used in react?

Inline If with Logical && Operator It works because in JavaScript, true && expression always evaluates to expression , and false && expression always evaluates to false . Therefore, if the condition is true , the element right after && will appear in the output. If it is false , React will ignore and skip it.

How do you pass arguments on onClick in react?

In order to pass a value as a parameter through the onClick handler we pass in an arrow function which returns a call to the sayHello function. In our example, that argument is a string: 'James': ... return ( <button onClick={() => sayHello('James')}>Greet</button> ); ...

Can we pass multiple list to a component in react?

Pass Multiple Value Inside Component In React Using Props And Map. Sometimes we need to pass multiple values inside child component one by one. In this article, we are going to discuss how to call child component with looping props values, or pass multiple values one by one inside child components in React.


1 Answers

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>;
}
like image 161
Tholle Avatar answered Sep 18 '22 14:09

Tholle