我有以下Button.js
:
import styled from 'styled-components';
const Button = styled.button`
background: black;
border: none;
color: white;
outline: none;
${props => props.add && css`
background: palevioletred;
color: white;
`};
${props => props.delete && css`
background: palevioletred;
color: white;
`};
`;
export default Button;
但是当我运行它时,我在编译期间遇到以下错误:
./ src / components / Button.js第10行:'css'未定义no-undef 第15行:'css'未定义为no-undef
然后我使用这样的按钮:
<Button delete>Delete</Button>
<Button add>Add</Button>
我在这里做错了什么?
答案 0 :(得分:6)
import styled, { css } from 'styled-components'
所以你的代码应该是
import styled, { css } from 'styled-components';
const Button = styled.button`
background: black;
border: none;
color: white;
outline: none;
${props => props.add && css`
background: palevioletred;
color: white;
`};
${props => props.delete && css`
background: palevioletred;
color: white;
`};
`;
export default Button;
答案 1 :(得分:0)
最后通过:“找出来”
import styled from 'styled-components';
const Button = styled.button`
background: black;
border: none;
color: white;
outline: none;
${(props) => {
if (props.add) {
return 'background: green;'
} else if (props.delete) {
return 'background: red;'
} else {
return 'background: black;'
}
}}
`;
export default Button;
哪个有效...尽管在我上面的问题中我遵循了文档完全。
答案 2 :(得分:0)
只需将{css}导入顶部!之前的评论中已经提到了这一点。
import React from 'react';
import styled, { css } from 'styled-components';
//import './Button.css';
const Button = styled.button`
background: white;
border: 2px solid black;
font-size: 1.2rem;
color: black;
${props =>
props.primary &&
css`
backround: blue;
`}
`;
export default Button;