简体   繁体   English

无法在样式组件中使用 Button,这给了我错误

[英]Cant use Button in styled component,its giving me error

I am using styled-components in js file.When I try to change button colour based on its content its giving me this error:我在 js 文件中使用 styled-components。当我尝试根据其内容更改按钮颜色时,它给了我这个错误:

Warning: Prop `className` did not match. Server: "sc-pVTma fSTvVY" Client: "sc-bdvvaa vaKvs"
    at button
    at styled.button (http://localhost:3000/_next/static/chunks/pages/issues.js?ts=1617437771165:11202:19599)
    at div
    at Link (http://localhost:3000/_next/static/chunks/pages/issues.js?ts=1617437771165:1808:19)

react-dom.development.js:67 Warning: Received `false` for a non-boolean attribute `high`.

If you want to write it to the DOM, pass a string instead: high="false" or high={value.toString()}.

If you used to conditionally omit it with high={condition && value}, pass high={condition ? value : undefined} instead.

The code for the same is given below for reference":下面给出了相同的代码以供参考”:

import styled from 'styled-components';
const Button=styled.button`
border:none;
border-radius:10px;
padding:7px 10px;
font-size:12px;
background:${(props) => (props.high ? 'red':'orange')};
color:white;
`
Status:<Button high={card.priority === "high"}>{ card.priority } Priority</Button>

This works fine for me.这对我来说很好。 You can match this with your code for debugging.您可以将其与您的代码匹配以进行调试。

import "./styles.css";
import styled from 'styled-components';
import {useState} from "react";

const Button=styled.button`
  border:none;
  border-radius:10px;
  padding:7px 10px;
  font-size:12px;
  background:${(props) => (props.high ? 'red':'orange')};
  color:white;
`

export default function App() {
  const [priority, setPriority] = useState("High");
  return (
    <div className="App">
      <Button high={priority === "High"}>{ priority } Priority</Button>
      <button onClick={() => setPriority("Low")}>Low Priority</button>
      <button onClick={() => setPriority("High")}>High Priority</button>
    </div>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM