javascript - 当我不接受构造函数的参数时,如何反应知道我何时向组件发送道具
问题描述
React 是如何知道 props 的,我正在向组件发送 props,并且我不接受构造函数的参数,而是 render() 方法访问 props。
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
ReactDOM.render(
<React.StrictMode>
<App myApp={'myApp'}/>
</React.StrictMode>,
document.getElementById('root')
);
// this is app component
import React, {Component} from 'react';
class App extends Component {
constructor() {
super()
this.state = {};
console.log(this.props.myApp) // undefined
}
render() {
console.log(this.props.myApp) // 'myApp' how and why, i'm not accepting arguments in contrutor why and how render method know this.props.myApp
}
}
exports default App;
// this is my assumption what react doing
const obj = new App(props);
解决方案
你需要重建你的构造函数:
constructor(props) {
super(props);
console.log(this.props.myApp); // 'myApp'
}
更多信息在这里: 在 React 构造函数中调用 super() 有什么作用?
推荐阅读
- mysql - MariaDB 10.3 - 升级到 10.3 失败
- ios - AudioKit - 如何从麦克风获取实时 floatChannelData?
- swift - 程序未检测到与 Physics Body SpriteKit 的碰撞
- vba - 删除excel值并将单元格设置为空
- c++ - std::vector _M_start 和 _M_finish 相同
- laravel - 在 Laravel 中使用 Vue/Axios 加载当前用户
- jquery - 在 WP 中选择更改时更改 YouTube iFrame 源
- node.js - 检测用户是否在 Apache 或 NodeJS 中使用 proxypass
- spring-security - 过滤器无法访问的 Spring Security Endpoints
- sql - 按组的第一个值对值进行分组