英文:
How can I use the css prop in @emotion/react without importing /** @jsxImportSource @emotion/react */ in every file?
问题
I want to use the @emotion/react library so that I can use the css prop like in this example:
<div
css={{
backgroundColor: 'pink',
height: '500px',
width: '500px',
}}
></div>
However, I do not want to import /** @jsxImportSource @emotion/react */
at the top of every file that is using the css prop.
Please provide me a step-by-step workaround to be able to have a running react project using the css prop without the strange import, using any of the following:
create-react-app, create-react-app + typescript, vite, vite + typescript
Thank you!
英文:
I want to use the @emotion/react library so that I can use the css prop like in this example:
<div
css={{
backgroundColor: 'pink',
height: "500px",
width: "500px,
}}
>
</div>
However, I do not want to import /** @jsxImportSource @emotion/react */
at the top of every file that is using the css prop.
Please provide me a step by step work-around to be able to have a running react project using the css prop without the strange import, using any of the following:
create-react-app, create-react-app + typescript, vite, vite + typescript
Thanks you!
答案1
得分: 2
使用CRACO,我们可以覆盖CRA配置并包括babel插件/预设。
对于create-react-app:
npm i @emotion/react
npm i -D @emotion/babel-plugin @craco/craco
在项目根目录(与package.json旁边)创建一个名为craco.config.js
的文件,包含以下代码:
module.exports = {
babel: {
presets: [
[
"@babel/preset-react",
{ "runtime": "automatic", "importSource": "@emotion/react" }
]
],
plugins: ["@emotion/babel-plugin"]
}
}
更新package.json中的脚本:
"scripts": {
- "start": "react-scripts start",
+ "start": "craco start",
- "build": "react-scripts build",
+ "build": "craco build",
- "test": "react-scripts test",
+ "test": "craco test"
- "eject": "react-scripts eject"
}
对于TypeScript CRA,请确保在compilerOptions中的tsconfig中包含以下内容:
"jsx": "react-jsx",
"jsxImportSource": "@emotion/react"
现在测试你的代码,不需要导入应该可以正常工作。
<div
css={{
backgroundColor: 'pink',
height: "500px",
width: "500px",
'&:hover': {
backgroundColor: 'green'
},
'@media(max-width: 1000px)': {
backgroundColor: 'blue'
}
}}
>
英文:
Using CRACO we can override CRA configuration and include babel plugins/presets.
For create-react-app:
npm i @emotion/react
npm i -D @emotion/babel-plugin @craco/craco
create file called craco.config.js
at the root of the project (next to package.json), with the following code:
module.exports = {
babel: {
presets: [
[
"@babel/preset-react",
{ "runtime": "automatic", "importSource": "@emotion/react" }
]
],
plugins: ["@emotion/babel-plugin"]
}
}
update scripts in package.json:
"scripts": {
- "start": "react-scripts start",
+ "start": "craco start",
- "build": "react-scripts build",
+ "build": "craco build",
- "test": "react-scripts test",
+ "test": "craco test"
- "eject": "react-scripts eject"
}
for typescript CRA, make sure to include this in tsconfig inside compilerOptions
"jsx": "react-jsx",
"jsxImportSource": "@emotion/react"
Now test your code without imports and it should be working.
<div
css={{
backgroundColor: 'pink',
height: "500px",
width: "500px",
'&:hover': {
backgroundColor: 'green'
},
'@media(max-width: 1000px)': {
backgroundColor: 'blue'
}
}}
>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论