平面設(shè)計(jì)是干什么的工資一般多少洛陽(yáng)seo網(wǎng)絡(luò)推廣
在開發(fā)組件過程中,偶爾需要?jiǎng)討B(tài)的插入css,比如在在iframe中渲染組件后,iframe中是沒有樣式的,所以需要手動(dòng)插入樣式。
插入樣式
通常是在useLayoutEffect中動(dòng)態(tài)創(chuàng)建style
標(biāo)簽
useLayoutEffect(() => {if (!ref.current) {const style = document.createElement('style');document.head.append(style);ref.current = style;}ref.current.innerText = css;return () => {if (ref.current) {document.head.removeChild(ref.current);ref.current = undefined;}};}, [css]);
useStyle
useStyle使用一個(gè)動(dòng)態(tài)插入style的hook,將上面的代碼進(jìn)行了封裝,方便使用。