Javascript 反应17-无法读取属性';道具';未定义的

Javascript 反应17-无法读取属性';道具';未定义的,javascript,reactjs,react-native,Javascript,Reactjs,React Native,在教程中,我看到了这个.props用于获取和使用道具。 当我写下这些并传递数据时,数据是可以的,但对于获取它们,它向我展示了以下内容: 无法读取未定义的属性“props” 有什么问题吗?我如何访问它们 import '../styles/Navbar.css'; function Navbar() { return ( <h2>{this.props.name} God is dead!</h2> ); } export default

在教程中,我看到了
这个.props
用于获取和使用道具。 当我写下这些并传递数据时,数据是可以的,但对于获取它们,它向我展示了以下内容: 无法读取未定义的属性“props”

有什么问题吗?我如何访问它们

import '../styles/Navbar.css';

function Navbar() {
    return (
        <h2>{this.props.name} God is dead!</h2>
    );
}

export default Navbar;
import'../styles/Navbar.css';
函数Navbar(){
返回(
{这个.道具.名字}上帝死了!
);
}
导出默认导航栏;

您的功能组件不会使用道具对象。将
props
作为参数添加到组件中。由于功能组件是无瞬时的,
是未定义的,这是导致错误的原因,因此组件中没有
,也就是
道具

function Navbar(props) {
  return (
    <h2>{props.name} God is dead!</h2>
  );
}
功能导航栏(道具){
返回(
{props.name}上帝死了!
);
}
对于功能组件,在函数签名中对命名的道具进行分解也是很常见的

function Navbar({ name }) {
  return (
    <h2>{name} God is dead!</h2>
  );
}
函数导航栏({name}){
返回(
{name}上帝死了!
);
}
或者作为箭头函数

const NavBar = ({ name }) => <h2>{name} God is dead!</h2>;
const-NavBar=({name})=>{name}上帝死了!;

constructor(props){super(props)}然后您可以通过此命令访问它们。props@Trizin它不工作,我把我的代码。请再次检查。@trizin即使在类组件中,构造函数也不需要使用this.props。