简体   繁体   English

React - 如何访问传递的道具 object 名称

[英]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?如何从组件内的 props 中获取foo的 props object 名称?
For example, so that I can use it in a function like:例如,这样我就可以在 function 中使用它,例如:

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"] .您应该能够使用Object.keys(props)来做到这一点,这将返回["foo"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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