javascript - 将 JSX 内容移动到单独的文件时,它无法正确呈现
问题描述
假设我有这个:
// Parent component:
export default function() {
return (
<React.Fragment>
<Toolbar slot="fixed" bottom>
This will be fixed to bottom of page
{order.name}
</Toolbar>
</React.Fragment>
)
}
我想用尽可能少的代码制作父组件 - 共享为小切口,因此工具栏将位于子组件中,因此它看起来像这样:
// Parent component:
export default function() {
return (
<React.Fragment>
<MyAwesomeToolbar order={order} />
</React.Fragment>
)
}
// MyAwesomeComponent:
export default function(self) {
let { order } = self.props
return (
<Toolbar slot="fixed" bottom>
This will be fixed to bottom of page
{order.name}
</Toolbar>
)
}
在第一个示例中 - 当工具栏实际上是在父组件中硬编码时,一切正常 - 工具栏位于页面底部。但是当使用第二种方式时,工具栏只是不在底部,而是浮动在页面中间,没有固定属性。
我试图使用类或只是一个简单的渲染文件(.JSX)来制作一个组件。两者都没有工作。
如何渲染具有与父组件相同的属性和样式的子组件?
解决方案
如果你移动它会起作用<React.Fragment />
吗?
// Parent component:
export default function() {
return (
<MyAwesomeToolbar order={order} />
);
}
// MyAwesomeComponent:
export default function(self) {
let { order } = self.props;
return (
<React.Fragment>
<Toolbar slot="fixed" bottom>
This will be fixed to bottom of page
{order.name}
</Toolbar>
</React.Fragment>
);
}
推荐阅读
- html - 垫抽屉内容滚动无法正常工作
- java - 编写此正则表达式的更好方法
- laravel - 如何构建 Laravel 生产环境
- laravel - 为什么将个人访问客户端 ID 和密码保存在 .env 中?
- ios - 如何使 AVAudioSession 在静音模式下工作并使用 .mixWithOthers
- html - 一些 div 不显示边框
- android - Gradle Build 在 Android Studio 中增加 versionCode 失败
- graphql - 以列表为输入的石墨烯突变
- r - 使用 R 中的插入符号包将数据缩放到 [-1 1] 范围
- php - 使用php解析器获取带有类的ap标签中的所有孩子