B
B
Bogdnan1232020-07-25 18:09:05
JavaScript
Bogdnan123, 2020-07-25 18:09:05

How to pass an object to a function in React?

Hi guys, I ran into such a problem:
For example, there is an object

let user = {
  age: 22,
  name: 'Boris'
}


How can I pass an example user.ageto a function to use it as a parameter in the future?

// здесь передаем
  let getUserData = () => {
    getUserParams(user.age)
  }
  // тут получаем как параметр
  let getUserParams = (age) => {
    // используем параметр
  }


If you do as I indicated above, then user.age === undefined

In React it looks like this:

// component
const UserComponent = ({user}) => {
    // вызываем здесь console.log(user.age) === 22
    let getUserData = () => {
      getUserParams(user.age) // если вызвать console.log(user.age) здесь то === андефайнед
    }

  return(
    //code
  )
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
L1nks, 2020-07-25
@L1nks

So everything works. Show all your code that doesn't work

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question