html - 通过 props 将 CSS 类传递给子组件

标签 html css reactjs styling

所以,我有一个带有确定类的 css 文件,比方说(类 1、类 2、类 3 等),我需要制作一个展示组件,将其属性传递给子组件。如果在子组件中调用名称,则可以使用 css 类的属性。

最佳答案

props.children = 嵌套在该组件内的组件。我们可以使用 React.cloneElement() 将属性传递给 child 。

const PresentationalComponent = ({ children }) => {
  const hardCodedClassName = '.some-class';
  return (
    <>
        {React.cloneElement(props.children, { className: hardCodedClassName })}
    </>
  )
}

// Usage

<PresentationalComponent>
  <ChildComponent />
</PresentationalComponent>

关于html - 通过 props 将 CSS 类传递给子组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59184589/

相关文章:

jquery - 将来自 json 响应的 JSON 值放入 HTML 元素

HTML 表格行 (tr) : How to set the height fixed, 等于最大高度元素

javascript - 窗口 D3 图表的跨度宽度

reactjs - 与未转换为 localIdentName 的 Storybook 连接字符串 react

php - html/php硬刷新

html - 在我的 html 页面底部居中放置下一个和上一个按钮

reactjs - 如何对静态类成员使用查找类型?

reactjs - 如何在 FlowType 中使用 React$Element?

javascript - Bootstrap collapse.in 需要点击两次才能隐藏侧边栏

html - CSS div(相对内的绝对)