使用您的陣營程序是自舉injectGlobal()
風格組分的輔助方法。使用這種方法,您可以設置任何CSS選擇器的樣式,就好像您使用傳統的CSS一樣。
首先創建一個JS文件導出文本與CSS的react-transition-group
模板(請不是我使用V2.1新的類名語法):上然後
globalCss.js
const globalCss = `
.transition-classes {
/* The double class name is to add more specifity */
/* so that this CSS has preference over the component one. */
/* Try removing it, you may not need it if properties don't collide */
/* https://www.styled-components.com/docs/advanced#issues-with-specificity */
&-enter&-enter {
}
&-enter&-enter-active {
}
&-exit&-exit {
}
&-exit&-exit-active {
}
}
`;
export default globalCss;
您入口點文件:
index.jsx
import { injectGlobal } from "styled-components";
import globalCss from "./globalCss.js";
injectGlobal`${ globalCss }`; // <-- This will do the trick
ReactDOM.render(
<Provider store={ Store } >
<HashRouter >
<Route path="/" component={ Component1 } />
<Route path="/" component={ Component2 } />
</HashRouter>
</Provider>,
document.getElementsByClassName("react-app")[0]
);
但是,即使使用樣式化組件,如果您只是使用CSS/SASS/Less編寫react-trasition-group
的類,它也可以很好地工作。