[英]Access props of imported component in React
我如何传递/访问导入组件的道具? 这个示例组件来自第三方库,所以我不确定如何获取它的 props go
import { Example } from './comps'
const App = () => {
return (
<Example />
);
console.log(Example.props) //this returns undefined
}
export default App;
您不能以这种方式从父组件访问子组件的道具。
而且:
const App = () => {
return ( // return will finish the function, so the console.log below doesn't make any sense.
<Example />
);
console.log(Example.props) //this returns undefined
}
在您的示例中, props 是从App
传递的东西,因此您可以直接从App
传递/访问:
const App = () => {
const [value, setValue] = useState(0);
return (
<Example number={value} setNumber={setValue} />
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.