亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

樣式反應組件 (SVG) 與樣式化組件

樣式反應組件 (SVG) 與樣式化組件

一只斗牛犬 2022-09-23 09:41:01
有沒有辦法按照這種樣式對導入到 react 中的 SVG 組件進行樣式設置:使用帶樣式的組件?我一直在將我的個人項目切換到帶樣式的組件,并且由于某種原因,樣式無法在組件上顯示。import { ReactComponent as Icon } from "./resources/somerandom.svg"這是我嘗試過的:import { ReactComponent as Icon } from "./resources/somerandom.svg"import styled from "styled-components"const IconWrapper = styled.svg`     fill: white;     width: 20px;`const RandomComponent = () => (<IconWrapper>  <Icon/></IconWrapper>)我知道我可以直接將樣式傳遞給SVG組件,但我想要一個使用帶樣式組件的解決方案。如果這篇文章沒有多大意義,那可能是因為我起床的時間比我記得的要長。感謝所有的幫助。
查看完整描述

1 回答

?
紅糖糍粑

TA貢獻1815條經驗 獲得超6個贊

給定 SVG:


<svg width="24" height="24" viewBox="0 0 24 24" fill="#868686" xmlns="http://www.w3.org/2000/svg">

<path d="M12 2.2467C9.62547 2.2468 7.32846 3.09181 5.51996 4.63055C3.71146 6.16928 2.5095 8.30132 2.12913 10.6452C1.74876 12.989 2.21481 15.3918 3.4439 17.4235C4.67298 19.4551 6.58488 20.9832 8.83752 21.7342C9.33752 21.8217 9.52502 21.5217 9.52502 21.2592C9.52502 21.0217 9.51251 20.2342 9.51251 19.3967C7 19.8592 6.35 18.7842 6.15 18.2217C5.92807 17.6747 5.57627 17.1898 5.125 16.8092C4.775 16.6217 4.275 16.1592 5.11249 16.1467C5.43227 16.1814 5.73898 16.2927 6.00663 16.4711C6.27427 16.6495 6.49496 16.8899 6.65 17.1717C6.78677 17.4174 6.97068 17.6337 7.19119 17.8082C7.4117 17.9827 7.66447 18.112 7.93503 18.1886C8.20559 18.2652 8.48861 18.2877 8.76788 18.2548C9.04714 18.2219 9.31717 18.1342 9.56248 17.9967C9.60577 17.4883 9.83234 17.013 10.2 16.6592C7.975 16.4092 5.65 15.5467 5.65 11.7217C5.63594 10.7279 6.00268 9.76631 6.675 9.03423C6.36928 8.17045 6.40505 7.22251 6.775 6.38423C6.775 6.38423 7.61247 6.12172 9.525 7.40923C11.1613 6.9592 12.8887 6.9592 14.525 7.40923C16.4375 6.10923 17.275 6.38423 17.275 6.38423C17.645 7.2225 17.6808 8.17046 17.375 9.03423C18.0493 9.76505 18.4164 10.7275 18.4 11.7217C18.4 15.5592 16.0625 16.4092 13.8375 16.6592C14.0761 16.9011 14.2599 17.1915 14.3764 17.5107C14.4929 17.83 14.5393 18.1705 14.5125 18.5092C14.5125 19.8468 14.5 20.9217 14.5 21.2592C14.5 21.5217 14.6875 21.8342 15.1875 21.7342C17.4362 20.9771 19.3426 19.4455 20.5664 17.4127C21.7903 15.38 22.2519 12.9785 21.8689 10.6369C21.4859 8.29535 20.2832 6.16607 18.4755 4.62921C16.6678 3.09235 14.3727 2.24794 12 2.2467Z"/>

</svg>

我們想要定位一些SVG屬性,有很多方法:


import { ReactComponent as Icon } from "./github.svg";

import styled from "styled-components";


// With wrapper, target the svg

const IconWrapper = styled.div`

  svg {

    width: 500px;

    height: 200px;

    fill: blue;

  }

`;


// Style the component (treated like styled.svg in this case)

const StyledIcon = styled(Icon)`

  width: 500px;

  height: 200px;

  fill: palevioletred;

`;


// With wrapper, target the generated className

const IconWrapperTargetClassname = styled.div`

  ${StyledIcon} {

    fill: palegreen;

  }

`;


ReactDOM.render(

  <React.StrictMode>

    <>

      <IconWrapper>

        <Icon />

      </IconWrapper>

      <StyledIcon />

      <IconWrapperTargetClassname>

        <StyledIcon />

      </IconWrapperTargetClassname>

    </>

  </React.StrictMode>,

  document.getElementById("root")

);

http://img1.sycdn.imooc.com//632d0ecd0001f0e303770622.jpg

查看完整回答
反對 回復 2022-09-23
  • 1 回答
  • 0 關注
  • 144 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號