K
K
Kirill Rezanov2020-09-27 12:53:59
React
Kirill Rezanov, 2020-09-27 12:53:59

What's the best way to get props into React components?

Hello everyone, it became interesting, such a philosophical question, what do you think is the best way to get props in components? Immediately destructure them and use them as ordinary variables:

const App = ({ menu, user, posts }) => {
  return (
    <BrowserRouter>
      <div>
        <Header />
        <div className="App">
          <Sidebar items={menu} />
          <Route path='/profile' render={ () => <Main user={user} posts={posts} /> } />
          <Route path='/messages' render={ () => <Dialogs /> } />
        </div>
      </div>
    </BrowserRouter>
  );
}

Or pass one variable to the props input and receive data through, for example: props.user?
For example:
const App = (props) => {
  return (
    <BrowserRouter>
      <div>
        <Header />
        <div className="App">
          <Sidebar items={props.menu} />
          <Route path='/profile' render={ () => <Main user={props.user} posts={props.posts} /> } />
          <Route path='/messages' render={ () => <Dialogs /> } />
        </div>
      </div>
    </BrowserRouter>
  );
}

Everywhere I see the use of props, but few people destructure the incoming data array, what do you think about this?

I immediately use destructuring and find it a very handy thing

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
abberati, 2020-09-27
@abberati

Not to transmit, but to receive. It is transmitted from the outside and received from the inside. Do it the way you like it, the way you like it.

R
Robur, 2020-09-28
@Robur

Yes, it doesn't matter.
This is not a philosophical question, but a matter of accepted style.
I write this way and that, if for some reason props are needed as an object, then I don’t destructure, if only values ​​are needed - with destructuring.

I immediately use destructuring and find it a very handy thing

Keep going, it's okay.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question