我正在尝试在 React 中创建一个无状态组件,其唯一目的是充当可重用的包装器。我也在使用 CSS 模块,因为我想要完全模块化的 CSS。
问题是我不想添加不必要<div>
的元素(甚至更多),而是我想使用 React 的片段。
现在,我遇到的问题是片段(至少现在)不接受类名。所以如果我试试这个:
// 在 Wrapper.js 中:
import React, { Fragment } from 'react'
import styles from './Wrapper.css'
const wrapper = (props) => (
<Fragment className={styles.wrapper}>
{props.children}
</Fragment>
)
export default wrapper
在(例如)Navbar.js 中:
import React from 'react'
import styles from './Navbar.css'
import Wrapper from '../../Layout/Wrapper'
const navBar = (props) => (
<nav className={styles.navBar}>
<Wrapper>
This is the site's main navigation bar.
</Wrapper>
</nav>
)
export default navBar
现在我当然可以使用 div 而不是 Fragment,但是有没有其他解决方法可以避免使用不必要的标记,我在晚上的这个时间完全不知道这些?:)
提前感谢您提供任何见解、建议、更正或任何其他形式的帮助!