【问题标题】:Warning: Received `false` for a non-boolean attribute. How do I pass a boolean for a custom boolean attribute?警告:收到非布尔属性的“假”。如何为自定义布尔属性传递布尔值?
【发布时间】:2018-09-21 21:11:47
【问题描述】:
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 的图片。

passing boolean custom attr as "comingsoon"

styled-components css props

【问题讨论】:

    标签: reactjs styled-components react-16


    【解决方案1】:

    试试这个:

    comingsoon={value ? 1 : 0}
    

    【讨论】:

    • 这对我有用。为什么这行得通而不是布尔值?
    • 这个接受的答案不正确,请向下滚动到 transient-props。参考:styled-components.com/docs/api#transient-props.
    • 这对我来说是有效的:resettable={condition ? true : undefined}
    【解决方案2】:

    5.1 开始,您现在可以使用transient props ($),它可以防止将道具传递给 DOM 元素。

    const Comp = styled.div`
      color: ${props =>
        props.$draggable || 'black'};
    `;
    
    render(
      <Comp $draggable="red" draggable="true">
        Drag me!
      </Comp>
    );
    

    【讨论】:

    • 我每两周左右就会回到这个问题,因为我经常遇到这个问题,我什至在下面写了一个替代方案。但这必须是最好的解决方案,因为 DOM 拒绝布尔值。
    • 控制台出现以下错误。 ``` 警告:无效的属性名称:$datasuccess ```
    【解决方案3】:

    您必须在属性中添加$ 前缀:

    $comingsoon={value}
    

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

    【讨论】:

      【解决方案4】:

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

      通常传递attribute={false} 可以,但不能传递给原生元素。

      【讨论】:

        【解决方案5】:

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

        comingsoon={value ? 1 : undefined}
        

        【讨论】:

          【解决方案6】:

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

          【讨论】:

          【解决方案7】:

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

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

          有两种解决方案:

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

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

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

          代码沙盒: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;
          
          

          【讨论】:

            【解决方案8】:

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

            const childrenWithProps = React.Children.map(props.children, child => {
              return React.cloneElement(child, { showcard: { showCard } }
            )});
            

            【讨论】:

              【解决方案9】:

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

              【讨论】:

                猜你喜欢
                • 2021-06-30
                • 2022-10-13
                • 1970-01-01
                • 1970-01-01
                • 2021-02-06
                • 2023-02-09
                • 2019-08-18
                • 2019-10-05
                • 1970-01-01
                相关资源
                最近更新 更多