如何使用样式的组件和ReactJ添加自定义CSS?警告:因非树立属性而被收到false

发布于 2025-02-11 10:50:37 字数 1631 浏览 1 评论 0原文

我喜欢根据道具添加样式。

import   {ArrowBackIcon} from './styles';

const App = () => {
...

  const isFirstImageAttachment = (index) => {
    return (
      filteredImages.length &&
      filteredImages[0]?.uuid === attachments[index]?.uuid
    );
  };

...

  return (
    <div className="App">
     ...

      <ArrowBackIcon
        isfirstimageattachment={isFirstImageAttachment(idx)}
        onClick={clickBackAttachment}
      />

      ...
    </div>
  );
};

)

styles.js

export const ArrowForwardIcon = styled(ForwardArrowIcon)`
  ...
  display: ${props => (props.isfirstimageattachment ? 'none' : 'block')}; ;
`;

isfirstimageattachment应该返回truefalse,因此取决于我喜欢隐藏或显示arrowforwardicon >组件。

我会遇到一个错误,说警告:非先进属性的false isfirstimageattachment。如果您想将其写入DOM,请改用一个字符串:IsfirstimageatTachment =“ false”或isfirstimagageattachment = {value.toString(value.toString(value.tostring(value.tostring)) )}

我不想做isfirstimageattachment =“ false”,因为isfirstimageattachment并不总是返回false。另外,此函数不会返回字符串 so isfirstimageatTachment = {value.tostring()}}不按我想要的方式工作。

我能做什么?

我发现的尝试

是,即使我传递了字符串false样式没有改变。

  return (
    <div className="App">
     ...

      <ArrowBackIcon
        isfirstimageattachment="false"
        onClick={clickBackAttachment}
      />

      ...
    </div>

我看到元素被应用了显示:无; 这不是将道具提供给样式组件的方式吗?

I like to add styles based on the props.

import   {ArrowBackIcon} from './styles';

const App = () => {
...

  const isFirstImageAttachment = (index) => {
    return (
      filteredImages.length &&
      filteredImages[0]?.uuid === attachments[index]?.uuid
    );
  };

...

  return (
    <div className="App">
     ...

      <ArrowBackIcon
        isfirstimageattachment={isFirstImageAttachment(idx)}
        onClick={clickBackAttachment}
      />

      ...
    </div>
  );
};

)

styles.js

export const ArrowForwardIcon = styled(ForwardArrowIcon)`
  ...
  display: ${props => (props.isfirstimageattachment ? 'none' : 'block')}; ;
`;

isFirstImageAttachment is supposed to return true or false so depending on that value I like to hide or show ArrowForwardIcon component.

I'm getting an error saying Warning: Received false for a non-boolean attribute isfirstimageattachment.If you want to write it to the DOM, pass a string instead: isfirstimageattachment="false" or isfirstimageattachment={value.toString()}

I don't want to do isfirstimageattachment="false" because isfirstimageattachment doesn't always return false. Also this function doesn't return string so isfirstimageattachment={value.toString()} don't work the way I want to.

Is there any I can do?

Attempts

What I figured out is even though I passed string false style didn't change.

  return (
    <div className="App">
     ...

      <ArrowBackIcon
        isfirstimageattachment="false"
        onClick={clickBackAttachment}
      />

      ...
    </div>

I saw the element got applied display: none;
Is this not the way giving the props to styled component?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟沫凡尘 2025-02-18 10:50:37
import   {ArrowBackIcon} from './styles';

const App = () => {
...

  const isFirstImageAttachment = (index) => {
     const showEl = filteredImages.length &&
      filteredImages[0]?.uuid === attachments[index]?.uuid;
     return showEl ? 'hidden' : 'visible';
  };

...

  return (
    <div className="App">
     ...

      <ArrowBackIcon
        isfirstimageattachment={isFirstImageAttachment(idx)}
      />

      ...
    </div>
  );
};

)

styles.js

export const ArrowBackIcon = styled(BackArrowIcon)`
  visibility: ${props => props.isfirstimageattachment};
`;

我能够以这种方式动态添加样式!

import   {ArrowBackIcon} from './styles';

const App = () => {
...

  const isFirstImageAttachment = (index) => {
     const showEl = filteredImages.length &&
      filteredImages[0]?.uuid === attachments[index]?.uuid;
     return showEl ? 'hidden' : 'visible';
  };

...

  return (
    <div className="App">
     ...

      <ArrowBackIcon
        isfirstimageattachment={isFirstImageAttachment(idx)}
      />

      ...
    </div>
  );
};

)

styles.js

export const ArrowBackIcon = styled(BackArrowIcon)`
  visibility: ${props => props.isfirstimageattachment};
`;

I was able to add style dynamically this way!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文