【问题标题】:React emotion styled not working but normal css was working fine for input and label styles反应情感样式不起作用,但正常的 CSS 对输入和标签样式工作正常
【发布时间】:2021-02-20 04:28:37
【问题描述】:

除了我尝试过许多不同方法的这件作品外,一切都在工作......

    const HiddenInput = styled.input`
    display: none;
    &:checked + CheckboxLabel: {
        background-color: #866dce;
    },
`

选中的应该会改变颜色,但不会。我也尝试使用 .checkbox-label where classname='checkbox-label" 但没有运气

这里是相关代码...

const CheckboxLabel = styled.label`
    background-color: rgb(211, 106, 106);
    cursor: pointer;
    font-size: 20;
    text-align: center;
    display: flex;
    justify-content: center;
    margin-right: 10px;
    padding: 5px;
    border-radius: 5px;
`
const HiddenInput = styled.input`
    display: none;
    &:checked + .checkbox-label: {
        background-color: #866dce;
    }
`

<HiddenInput type="checkbox" id={"toggle"+index} onChange={() => handleChecked(item)} key={"input" + index} />
<CheckboxLabel htmlFor={"toggle"+index} className="checkbox-label" key={"label" + index}>{item}</CheckboxLabel><br />

【问题讨论】:

    标签: javascript css reactjs styles emotion-js


    【解决方案1】:

    您需要用${} 包装组件。还有一个 scss 语法错误,您必须从选择元素的前面删除 :

        const HiddenInput = styled.input`
          display: none;
          &:checked + ${CheckboxLabel} {
            background-color: #866dce;
          },
    `
    

    注意:您可以使用默认包含在 create-react-app 中的 babel-macro

    import styled from '@emotion/styled/macro'
    

    【讨论】:

    • 我试过了,但我得到了这个错误...未捕获的错误:组件选择器只能与 babel-plugin-emotion 结合使用。我认为这是为了不同的进口。我的是从“@emotion/styled”导入样式的
    • @JustinOberte emotion.sh/docs/@emotion/babel-plugin 我认为您的依赖项中缺少您。但是,如果您更新到版本 8,它应该可以正常工作
    • 嗯,我安装了插件,但我仍然收到同样的错误
    • "dependencies": { "@emotion/core": "^10.1.1", "@emotion/styled": "^10.0.27", "@testing-library/jest-dom ": "^5.11.5", "@testing-library/react": "^11.1.0", "@testing-library/user-event": "^12.1.10", "axios": "^0.21 .1”、“plotly.js”:“^1.58.4”、“react”:“^17.0.1”、“react-dom”:“^17.0.1”、“react-plotly.js”:“ ^2.5.1", "react-router-dom": "^5.2.0", "react-scripts": "4.0.0", "web-vitals": "^0.2.4" },
    猜你喜欢
    • 1970-01-01
    • 2014-10-05
    • 2021-10-28
    • 2015-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    相关资源
    最近更新 更多