reactjs - React Props 不在 UI 上显示数据
问题描述
我正在学习反应
在处理 Props 时,我创建了一个组件并在我的 index.jsx 中使用该组件。但是通过 props 传递的值不会显示在 UI 上。
使用props.jsx
import React from 'react';
class UsingProps extends React.Component {
render() {
return (
<div>
<p>{this.props.headerProp}</p>
<p>{this.props.contentProp}</p>
</div>
);
}
}
export default UsingProps;
索引.jsx
import React from 'react';
import UsingProps from './Props/UsingProps.jsx';
class App extends React.Component {
render() {
return (
<div>
<UsingProps />
</div>
);
}
}
const myElement = <App headerProp="Header from props!!!" contentProp="Content from props!!!" />;
ReactDOM.render(myElement, document.getElementById('root'));
export default App;
解决方案
您将 放在headerProp
组件上App
,而不是UsingProps
组件上,这是您尝试访问它的地方。您需要将其修改为:
class App extends React.Component {
render() {
return (
<div>
<UsingProps headerProp="Header from props!!!" contentProp="Content from props!!!" />
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('root'));
推荐阅读
- flutter - 如何将 controller.index 值保存到静态变量中?
- python - 嵌套 For 循环和 nltk.FreqDist - IndexError:列表索引超出范围
- python - 熊猫插值条件
- reactjs - 无法在 fetch api 中将输入文本插入到 url
- android - 某些设备上使用颤振和火焰动画的错误
- ios - 将 React Native 警报定位在屏幕底部
- python - Python中用于替换列表中元素的函数
- python - 我无法在这个 pygame 太空侵略者中开枪
- entity-framework - 无法使用 EF Core 6 添加迁移
- powerapps - Power Apps 导航功能