简体   繁体   中英

React - How to access passed props object name

In react, if I have a component that looks something along the lines of this:

const SomeComponent = (props) => {
    return stuff
}

and I pass in props like this:

<SomeComponent foo={"x"}/>

How can I get the props object name of foo from the props within the component?
For example, so that I can use it in a function like:

const useObjectName = (xyz) => {
  if (xyz){
    //do something 
  }
}
const SomeComponent = (props) => {
  const theObjectName = ???
  useObjectName(theObjectName)
  return stuff
}

You should be able to do that using Object.keys(props) which will return ["foo"] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM