81
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()}.

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

我正在使用 styled-components 并通过组件传递属性。这是我如何通过 attr 的图片。

将布尔自定义属性传递为“即将到来”

样式组件 css 道具

4

9 回答 9

85

试试这个:

comingsoon={value ? 1 : 0}
于 2018-06-08T01:13:42.170 回答
54

5.1现在可以使用瞬态 props ( $) 来防止 props 被传递给 DOM 元素。

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

render(
  <Comp $draggable="red" draggable="true">
    Drag me!
  </Comp>
);
于 2020-10-01T14:34:15.523 回答
23

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

$comingsoon={value}

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

于 2020-10-14T17:04:40.363 回答
10

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

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

于 2020-04-10T05:53:04.300 回答
7

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

comingsoon={value ? 1 : undefined}
于 2019-08-15T13:44:22.913 回答
6

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

于 2018-04-12T00:37:18.690 回答
3

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

这在styled-components此处的存储库中有很好的记录:https ://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;

于 2020-10-24T04:04:37.703 回答
1

通过用括号括住我正在通过{}的变量来解决这个问题。booleanprops

const childrenWithProps = React.Children.map(props.children, child => {
  return React.cloneElement(child, { showcard: { showCard } }
)});
于 2021-04-08T07:08:23.110 回答
0

如果样式化组件的属性具有在 HTML 中存在的名称,也会导致此警告。例如,我在命名 property 时遇到了这样的问题wrap。重命名后警告消失。

于 2022-02-13T21:03:17.050 回答