reactjs - React 中的函数组件有 props 吗?

标签 reactjs react-props react-component react-functional-component react-class-based-component

我们可以在 React 的函数组件中使用 props 吗?如果可以,怎么做?我正在尝试这样做

console.log(this.props) 

在函数组件中的返回函数之上,但它一直给我错误。

最佳答案

是的。您可以直接在函数组件内传递 props(使用 props),如下所示:

function myFunc(props){
  console.log(props.content);
  return <div>{props.content}</div>;
}

假设传入的props具有元素content

澄清一下,函数组件中没有 this 因为它们根本不是类!相反,您可以将 props 直接传递到函数中。

事实上,这可能是您在 React 中首先学到的东西之一。所以如果你是新手,那么我建议 the tutorial here .

关于reactjs - React 中的函数组件有 props 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62541722/

相关文章:

javascript - 当变量初始化为 props 更改时,React props 意外更改

reactjs - TypeScript:接口(interface)不能同时扩展两种类型

javascript - cfs 集合在 react.component 中显示错误

javascript - React + TypeScript : Passing React Component as Props, 使用 props 渲染 [错误:TS2339]

css - 如何根据最大选项宽度设置 react 选择宽度?

javascript - 在jsx中使用split和map

javascript - react js : multiple child comments

javascript - 如果我在特定的主体类上引用它,CSS 模块声明将不起作用

javascript - Reactjs 到 firebase 数据库 : Trouble adding multiple children to firebase db

reactjs - 找不到模块 : Can't resolve 'bootstrap/dist/css/bootstrap.css' in 'C:\Users\test\counter-app\src'