Reactjs 有没有一种方法可以用不同的主元素声明样式化组件?
我需要创建两个类似的样式组件,它们将共享它们的样式,但使用不同的HTML元素 有一种方法可以在运行时使用“as”属性(),但我有一个特定的导出,我需要使用默认值,但我需要它以一种不使用默认值的方式,以便链接样式化组件为“styled.a”,StyledLink组件为“styled.span”,我尝试了以下操作:Reactjs 有没有一种方法可以用不同的主元素声明样式化组件?,reactjs,styled-components,Reactjs,Styled Components,我需要创建两个类似的样式组件,它们将共享它们的样式,但使用不同的HTML元素 有一种方法可以在运行时使用“as”属性(),但我有一个特定的导出,我需要使用默认值,但我需要它以一种不使用默认值的方式,以便链接样式化组件为“styled.a”,StyledLink组件为“styled.span”,我尝试了以下操作: export const StyledLink = styled.span(` color: ${props => props.theme.colors.mainLinkCol
export const StyledLink = styled.span(`
color: ${props => props.theme.colors.mainLinkColor};;
text-decoration: underline;
&:hover {
color: ${props => props.theme.colors.textAccent};
text-decoration: underline;
}
`);
export const Link = <StyledLink as={RRLink} />;
export const StyledLink=styled.span(`
颜色:${props=>props.theme.colors.mainLinkColor};;
文字装饰:下划线;
&:悬停{
颜色:${props=>props.theme.colors.textAccent};
文字装饰:下划线;
}
`);
导出常量链接=;
这显然不起作用。。。那么,有没有一种方法可以让链接模仿StyledLink样式,但使用“a”标记而不是“span”?只需从
样式化组件导入并使用css
,如下所示:
import React from "react";
import ReactDOM from "react-dom";
import styled, { css } from 'styled-components';
const sharedStyles = css`
background-color: gold;
`;
const Div = styled.div`
${sharedStyles}
`;
const Button = styled.button`
${sharedStyles}
`;
const App = () => (
<>
<Div>Hello Div</Div>
<Button>Hello Button</Button>
</>
);
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
从“React”导入React;
从“react dom”导入react dom;
导入样式化,{css}来自“样式化组件”;
常量sharedStyles=css`
背景颜色:金色;
`;
const Div=styled.Div`
${sharedStyles}
`;
const Button=styled.Button`
${sharedStyles}
`;
常量应用=()=>(
你好,迪夫
你好按钮
);
const rootElement=document.getElementById(“根”);
render(,rootElement);
只需从样式化组件导入并使用css
,如下所示:
import React from "react";
import ReactDOM from "react-dom";
import styled, { css } from 'styled-components';
const sharedStyles = css`
background-color: gold;
`;
const Div = styled.div`
${sharedStyles}
`;
const Button = styled.button`
${sharedStyles}
`;
const App = () => (
<>
<Div>Hello Div</Div>
<Button>Hello Button</Button>
</>
);
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
从“React”导入React;
从“react dom”导入react dom;
导入样式化,{css}来自“样式化组件”;
常量sharedStyles=css`
背景颜色:金色;
`;
const Div=styled.Div`
${sharedStyles}
`;
const Button=styled.Button`
${sharedStyles}
`;
常量应用=()=>(
你好,迪夫
你好按钮
);
const rootElement=document.getElementById(“根”);
render(,rootElement);