gpt4 book ai didi

javascript - React 应用程序中带有动态文本的样式化组件

转载 作者:行者123 更新时间:2023-11-28 15:12:08 27 4
gpt4 key购买 nike

我正在开发 React 应用程序并使用样式化组件。到目前为止,一切进展顺利,我真的很喜欢它,但我在使我的样式化组件像我希望的那样可重用方面遇到了一些问题。

我觉得我的部分问题在于我仍在学习一些有关 React 和样式化组件的知识。

该应用程序是使用 react-boilerplate 构建的,因此我的组件标记基于他们的 example关于如何使用样式组件。我也在使用 Material UI。

import React from 'react';
import styled from 'styled-components';
import ListSubheader from 'material-ui/List/ListSubheader';

// styles for list subheader
const StyledListSubheade = styled(ListSubheader)`
&& {
color: #fff;
font-size: 0.75em;
line-height: 16px;
letter-spacing: 1px;
text-transform: uppercase;
padding: 12px 16px 20px;
}
`;

class ListSubheader extends React.Component {
render() {
return (
<StyledListSubheader>Title</StyledListSubheader>
);
}
}

export default ListSubheader;

很简单吧?但是,当我想在每个组件中更改 StyledListSubheader 中的文本时,我该怎么办?如果我想在两个不同的组件中导入 ListSubheader 并且我希望文本在一个组件中为 Title 而在另一个组件中为 Example 怎么办?如何动态更改该文本?

感谢任何帮助!谢谢!

最佳答案

使用 ListSubHeader 的 children 属性最容易做到这一点:

class ListSubheader extends React.Component {
render() {
return (
<StyledListSubheader>{this.props.children}</StyledListSubheader>
);
}
}

您现在可以在其他组件中将其用作:

<ListSubheader>Whatever you like</ListSubheader>

关于javascript - React 应用程序中带有动态文本的样式化组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48122350/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com