警告:对于非布尔属性,收到`false`.如何为自定义布尔属性传递布尔值?

des*_*jic 16 reactjs styled-components react-16

Warning: Received `false` for a non-boolean attribute `comingsoon`.

If you want to write it to the DOM, pass a string instead: 
comingsoon="false" or comingsoon={value.toString()}.
Run Code Online (Sandbox Code Playgroud)

如何在React的自定义属性中传递布尔值?

我正在使用样式组件并通过组件传递属性.这是我如何通过attr的图片.

将布尔自定义attr作为"comesoon"传递

样式组件css道具

Mus*_*ill 30

截至5.1您现在可以使用瞬态道具,以防止道具被传递给 DOM 元素。

const Comp = styled.div`
  color: ${props =>
    props.$draggable || 'black'};
`;

render(
  <Comp $draggable="red" draggable="true">
    Drag me!
  </Comp>
);
Run Code Online (Sandbox Code Playgroud)

  • 我在控制台上收到以下错误。``` 警告:属性名称无效:`$datasuccess` ``` (6认同)

小智 22

试试这个:

comingsoon={value ? 1 : 0}
Run Code Online (Sandbox Code Playgroud)

  • 这个接受的答案不正确,请向下滚动到 **transient-props**。参考:https://styled-components.com/docs/api#transient-props。 (21认同)
  • 这对我有用。为什么这个有效而不是布尔值? (19认同)
  • 这就是我的情况:`resettable={condition ? true : 未定义}` (2认同)
  • @EvgenyBobkin 是正确的,正确的方法是使用瞬态道具。请看另一个答案 (2认同)

小智 21

您必须$为属性添加前缀:

$comingsoon={value}
Run Code Online (Sandbox Code Playgroud)

Styled Components 在 5.1 版本中添加了瞬态道具:https : //styled-components.com/docs/api#transient-props


Fun*_*bat 9

就我而言,那是因为我不小心传入{...@props}了一个 div。

通常传递attribute={false}是好的,但不是原生元素。


Har*_*ang 8

此错误styled-components似乎是由于styled()尝试将布尔值应用于 DOM 中的元素,但 DOM 元素仅接受字符串作为属性。

这在此处的存储库中有详细记录styled-componentshttps://github.com/styled-components/styled-components/issues/1198

有两种解决方案:

  1. 将带有传递的属性的样式组件向上提升,以便该属性不会直接应用于元素。或者,

  2. 调用样式组件时,将传递的属性从 props 中过滤掉。

下面的代码演示了这两个选项。

CodeSandbox:https://codesandbox.io/s/cool-thunder-9w132?file=/src /App.tsx

import React, { useState } from "react";
import styled from 'styled-components';

// demonstration of two different solutions for solving the styled-components error:
// `Warning: Received `false` for a non-boolean attribute`
// Solution 1: Lift the attribute up into a wrapper.
// Solution 2: Filter-out the `toggle` attribute passed to styled-component.

interface BtnProps {
  toggle: boolean;
}

const Container = styled.div`
  width: 100%;
  height: 500px;
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  align-items: center;
`;

const StyledBtnOne = styled.div<BtnProps>`
  & button {
    background-color: ${({toggle}) => toggle ? ' #2ecc71' : '' };
  };
`;

const StyledBtnTwo = styled(({primary, ...props}) =>
  <button {...(({toggle, ...propz}) => propz)(props)} />)<BtnProps>`
  background-color: ${({toggle}) => toggle ? ' #3498db' : '' };
`;

const App = () => {

  const [ btnOne, setBtnOne ] = useState(false);

  const [ btnTwo, setBtnTwo ] = useState(false);

  const triggerOne = () => setBtnOne(!btnOne);

  const triggerTwo = () => setBtnTwo(!btnTwo);

  return (
    <Container>
      <StyledBtnOne toggle={btnOne}>
        <button 
          onClick={triggerOne}>
            Solution 1
        </button>
      </StyledBtnOne>

      <StyledBtnTwo 
        toggle={btnTwo}
        onClick={triggerTwo}>
        Solution 2
      </StyledBtnTwo>
    </Container>
  );

}

export default App;

Run Code Online (Sandbox Code Playgroud)


osk*_*are 6

类似于上面的 Frank Lins 回答,但我不得不使用 undefined 而不是 0 来消除警告:

comingsoon={value ? 1 : undefined}
Run Code Online (Sandbox Code Playgroud)


Cub*_*Eye 5

只需将其改为一个数字,这是来自https://github.com/styled-components/styled-components/issues/1198的解决方法:

  • 这会将值传递给 DOM,但对于样式组件来说,这并不是您真正想要的。尽管这是删除警告的简单方法。 (2认同)

小智 5

在布尔值之前添加“+”。

comingsoon = {+creator.comingSoon}
Run Code Online (Sandbox Code Playgroud)

下面的示例来自回答链接

import styled from "styled-components";
import { Link } from "react-router";

const StyledLink = styled(Link)`
  color: ${({ inverted }) => (inverted ? "white" : "chartreuse")};
`;

function Navbar() {
  return (
    <nav>
      {# Bad #}
      <StyledLink inverted={true}>Home</StyledLink>

      {# Good #}
      <StyledLink inverted={+true}>About</StyledLink>
    </nav>
  );
}
Run Code Online (Sandbox Code Playgroud)