所以,我有一个带有确定类的 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/