在React中,可以使用不同的方法来编辑组件的CSS。下面是一些常用的解决方法,包含代码示例:
import React from 'react';
const MyComponent = () => {
const myStyle = {
color: 'red',
fontSize: '20px',
fontWeight: 'bold'
};
return Hello World!;
};
export default MyComponent;
import React from 'react';
import styles from './MyComponent.module.css';
const MyComponent = () => {
return Hello World!;
};
export default MyComponent;
使用styled-components的示例:
import React from 'react';
import styled from 'styled-components';
const StyledDiv = styled.div`
color: red;
font-size: 20px;
font-weight: bold;
`;
const MyComponent = () => {
return Hello World! ;
};
export default MyComponent;
使用emotion的示例:
import React from 'react';
import { css } from '@emotion/react';
const myStyle = css`
color: red;
font-size: 20px;
font-weight: bold;
`;
const MyComponent = () => {
return Hello World!;
};
export default MyComponent;
这些方法都可以用来编辑React组件的CSS样式,选择适合你项目需求和个人偏好的方法即可。