insert style tags into react components
to insert a style tag in a react component, wrap our css in a template string in a style tag. the expression between the curly braces will be evaluated and the style will be added.
const app = () => {
const styles = `
.my-h2 {
padding: 25px;
background-color: salmon;
color: white;
}
`;
return (
<div><h2classname="my-h2">hello 迹忆客(jiyik.com)h2><style>{styles}style>div>
);
};
export default app;
the expression between the curly braces in the style tag will be evaluated and the style will be added.
it is also possible to inline css directly in the style tag.
const app = () => {
return (
<div><h2classname="my-h2">hello 迹忆客(jiyik.com)h2><style>{`
.my-h2 {
padding: 25px;
background-color: salmon;
color: white;
}
`}style>div>
);
};
export default app;
the code snippet above achieves the same result.
注意
, the styles we add using the style tag are not within the scope of the app component.
as long as the app component is rendered, another component can use the my-h2 class.
another way is to use a file with .css extension and write and import your global css file.
here's how we move our css into a file called app.css.
app.css
.my-h2 { padding: 25px; background-color: salmon; color: white; }
here is how we import the css file into the app.js file.
import './app.css';
const app = () => {
return (
<div><h2classname="my-h2">hello 迹忆客(jiyik.com)h2>div>
);
};
export default app;
this is the more common way to write and apply css to react components.
for reprinting, please send an email to 1244347461@qq.com for approval. after obtaining the author's consent, kindly include the source as a link.
article url:
related articles
publish date:2025/03/10 views:179 category:react
-
使用 useeffect 钩子来监听 react 中的状态变化。 我们可以将要跟踪的状态变量添加到挂钩的依赖项数组中,并且每次状态变量更改时都会运行 useeffect 钩子中的逻辑。
get parent height and width in react
publish date:2025/03/10 views:158 category:react
-
在 react 中获取父级的高度和宽度: 在元素上设置 ref 属性。 在 useeffect 钩子中,更新高度和宽度的状态变量。 使用 offsetheight 和 offsetwidth 属性来获取元素的高度和宽度。
get the value of an input field in react
publish date:2025/03/10 views:179 category:react
-
要在 react 中获取输入字段的值: 声明一个跟踪输入字段值的状态变量。 将 onchange 属性添加到输入字段。 使用 event.target.value 获取输入字段的值并更新状态变量。
rendering nested arrays in react using map()
publish date:2025/03/10 views:71 category:react
-
使用 map() 渲染嵌套数组: 使用 map() 方法迭代外部数组。 在每次迭代中,调用嵌套数组的 map()方法。 渲染嵌套数组的元素。
publish date:2025/03/10 views:118 category:react
-
在 react 中获取表单提交的输入值: 将输入字段的值存储在状态变量中。在表单元素上设置 onsubmit 属性。 在我们的 handlesubmit 函数中访问输入字段的值。
publish date:2025/03/10 views:130 category:react
-
在 react 中为 body 元素添加一个类: 在 useeffect 或事件处理程序中以 document.body 的形式访问 body 元素。 使用 classlist.add() 方法将类添加到 body 标记。 例如,document.body.classlist.add('my-class'
clearing the value of an input field in react.js
publish date:2025/03/10 views:119 category:react
-
要在 react 中清除输入字段的值:将输入的值存储在状态变量中。 当某个事件发生时,将状态变量设置为空字符串。对于不受控制的组件,将 ref 的值设置为空字符串,例如 ref.current.va
deleting keys from state object in react
publish date:2025/03/10 views:62 category:react
-
要从 react 状态对象中删除键: 使用 usestate 挂钩来存储状态对象。 解构对象的键和其余属性。 将状态设置为其余属性。
publish date:2025/03/09 views:170 category:react
-
要在 react 状态下替换数组中的对象:使用 map() 方法遍历数组。 在每次迭代中,检查是否满足某个条件。 替换满足条件的对象并按原样返回所有其他对象。