javascript - 将功能组件转换为基于类的组件
问题描述
我需要将功能组件转换为基于类的组件。我对 React 还很陌生,但我尝试过转换组件。我收到错误 - ReferenceError: Cant find variable: Props
我的问题是,我在哪里添加基于类的组件中存在的道具以使转换工作?
基于类的组件是一个模式,其表单从父组件触发,效果很好。该表单使用在基于类的组件中不起作用的状态变量,因此我需要将当前的功能组件转换为基于类的组件。我使用的是 16.6.3 版的 React,因为其他软件包不适用于较新版本的 React-Native,所以我不能在这个版本中使用钩子。
功能组件
const ShowModal = props => (
<Modal
visible={props.display}
animationType="slide"
onRequestClose={() => console.log("closed")}
>
...Other stuff in here.
</Modal>
);
export default ShowModal;
基于类的组件
export default class ShowModal extends Component {
state = {
};
render() {
return (
...Other stuff in here
);
}
}
我收到错误 - ReferenceError: Cant find variable: Props
解决方案
基于类的组件props
暴露在类的主要范围内。您应该使用this
关键字阅读它
class Component extends React.Component{
render(){return this.props.value}
}
推荐阅读
- android - 如何在drawables中创建一个带有三角形边的矩形形状?
- c# - Process.StartInfo.WorkingDirecotry 在设置 StartInfo.Verb = "runas" 以启动 .bat 文件(包含 cd 命令)时没有用
- ruby-on-rails - 添加设计密钥后发生未处理的低级错误
- java - firebase 数据库查询并返回值 true,但如果用户存在,仍会继续前进,并注册重复条目
- s4sdk - 使用 S/4 Hana Cloud SDK 和 Cloud SDK 创建父实体及其子实体
- node.js - 如何使用express js和sequelize将csv文件导入mysql数据库
- c++ - 从 C++ 中的 CLI 应用程序在 macOS 上发送通知
- json - 在 Postman 测试中,如何从动态 JSON 生成模式?
- django - Django Rest 框架 - ListCreateAPIView 不打印任何内容
- powershell - 使用 PowerShell(或其他脚本解决方案)检查 OneDrive 和 OneNote 客户端状态