Извлечение стилей в React + material-ui - PullRequest
0 голосов
/ 28 мая 2019

Я использую React + material-ui. Я хотел бы извлечь стили, определенные в каждом компоненте, в общий файл.

если в одном файле находятся следующие элементы, каждый работает хорошо: -

import { withStyles } from '@material-ui/core/styles';
...

const styles = theme => ({
    createButton: {
        [theme.breakpoints.up('md')]: {
            borderRadius: '10px',
            display: 'block',
            margin: '0 auto',
            marginTop: '10px',
            marginBottom: '10px',
            width: '360px',
        },
        [theme.breakpoints.down('sm')]: {
            borderRadius: '0px',
            bottom: '0px',
            position: 'relative',
            width: '100%',
            height: '60px',
            fontSize: '20px',
        },
        backgroundColor: theme.palette.secondary.main,
        color: 'white',
        '&:hover': {
            backgroundColor: theme.palette.secondary.main,
        },
    }
});
...

class Home extends Component {
...
<Button className={this.props.classes.createButton}>Hello</Button>
...
}

export default (withStyles(styles)(Home));

Теперь я хочу извлечь следующее в другой файл, например, common.js, затем в будущем я могу импортировать его в другие компоненты, чтобы можно было повторно использовать стиль: -

const styles = theme => ({
    createButton: {
        [theme.breakpoints.up('md')]: {
            borderRadius: '10px',
            display: 'block',
            margin: '0 auto',
            marginTop: '10px',
            marginBottom: '10px',
            width: '360px',
        },
        [theme.breakpoints.down('sm')]: {
            borderRadius: '0px',
            bottom: '0px',
            position: 'relative',
            width: '100%',
            height: '60px',
            fontSize: '20px',
        },
        backgroundColor: theme.palette.secondary.main,
        color: 'white',
        '&:hover': {
            backgroundColor: theme.palette.secondary.main,
        },
    }
});

тогда, когда я

import { styles } from './somewhere/common';

стиль createButton больше не работает.

упомянул

Attempted import error: 'styles' is not exported from './somewhere/common'.

1 Ответ

2 голосов
/ 28 мая 2019

Вам необходимо экспортировать styles из './somewhere/common', добавить это в конец файла:

export default styles;

и импортировать его, используя:

import styles from './somewhere/common'
...