Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript React Basics (retired) Thinking in Components Properties

How to pass more than one argument in components function

What if i want to pass more than one argument components function like this

function Anything(props, myprops) { return( <h1>{props.title}</h1> <p>{myprops.desc}</p> ); } ReactDOM.render(<Anything title="Hi" desc="Hola" />, document.getElementById("app");

The above code takes only the first argument not the second one

2 Answers

Of course you can use multiple properties. Like this:

class App extends React.Component {
  render () {
    return (
      <div>
        <h1>{this.props.title}</h1>
        <h2>{this.props.description}</h2>
      </div>
    );
  }
}

ReactDOM.render(
  document.getElementById('container'),
  <App title="Awesome App" description="This is my first Awesome app" />
);

You can also use props as an object...

function Comment(props) {
  return (
    <div className="Comment">
      <div className="UserInfo">
        <img className="Avatar"
          src={props.author.avatarUrl}
          alt={props.author.name}
        />
        <div className="UserInfo-name">
          {props.author.name}
        </div>
      </div>
      <div className="Comment-text">
        {props.text}
      </div>
      <div className="Comment-date">
        {formatDate(props.date)}
      </div>
    </div>
  );
}

Example taken from React Documentation https://reactjs.org/docs/components-and-props.html