简体   繁体   English

如何让我的 withStyles 类在我的 React 应用程序的导出 class 组件中工作

[英]How to get my withStyles classes to work in my export class component in my React app

I'm trying to apply styles to my React form.我正在尝试将 styles 应用于我的 React 表单。 I'm using withStytles from Material UI.我正在使用 Material UI 中的withStytles

The styles however are not taking into affect.然而,styles 并未生效。 I tried testing the code in my first <DialogContentText> in the code below, but it's not working.我尝试在下面的代码中测试我的第一个<DialogContentText>中的代码,但它不起作用。

EDIT: edited my code to reflect David's answer.编辑:编辑我的代码以反映大卫的回答。

import React, { Component, Fragment } from 'react';
import { withStyles } from '@material-ui/core/styles';
import Button from '@material-ui/core/Button';
import Dialog from '@material-ui/core/Dialog';
import DialogActions from '@material-ui/core/DialogActions';
import DialogContent from '@material-ui/core/DialogContent';
import DialogContentText from '@material-ui/core/DialogContentText';
import DialogTitle from '@material-ui/core/DialogTitle';
import IconButton from '@material-ui/core/IconButton';
import AddCircleIcon from '@material-ui/icons/AddCircle';
import TextField from '@material-ui/core/TextField';
import FormHelperText from '@material-ui/core/FormHelperText';
import Select from '@material-ui/core/Select';
import MenuItem from '@material-ui/core/MenuItem';
import InputLabel from '@material-ui/core/InputLabel';
import FormControl from '@material-ui/core/FormControl';
const styles = theme => ({
    formControl: {
      width: 500
    },
    selectEmpty: {
      marginTop: theme.spacing(2),
    },});
  

export default class extends Component {


    state = {
        open: false,
        bucket: {
            name:'',
            category: '',
            about: '',
        }
    }

    handleToggle = () => {
        this.setState({
            open: !this.state.open
        })
    }

    handleChange = name => ({ target: { value } }) => {
        this.setState({
            bucket:{
                ...this.state.bucket,
                [name]: value,
            }
        })
    }

    render() {
        const { open, bucket: { name, category, about } } = this.state;

        const { classes } = this.props;

        return <Fragment>
        <IconButton color="primary" size="large" onClick={this.handleToggle}>
            <AddCircleIcon/>
        </IconButton>
        <Dialog open={open} onClose={this.handleToggle} aria-labelledby="form-dialog-title">
        <DialogTitle id="form-dialog-title">Create your Bucket</DialogTitle>
        <DialogContent>
            <DialogContentText>
            Get started! Make your bucket by telling us a little bit more.
            </DialogContentText>
                <form>
                <TextField
                id="filled-password-input"
                label="Bucket Title"
                value={name}
                variant="filled"
                onChange={this.handleChange('name')}
                margin="normal"
                required = "true"
                className = {classes.formControl}
                />
                <br/>
                <br/>
                <FormControl>
                <InputLabel htmlFor="category"> What type of Bucket </InputLabel>
                <Select
                labelId="demo-simple-select-helper-label"
                id="demo-simple-select-helper"
                value={category}
                onChange={this.handleChange('category')}
                required = "true"
                >
                <MenuItem value={'personal'}> Personal </MenuItem>
                <MenuItem value={'social'}> Social </MenuItem>
                </Select>
                </FormControl>
                <FormHelperText>Is this your personal or social Bucket?</FormHelperText>
                <TextField
                id="filled-password-input"
                label="About"
                multiline
                rows="5"
                value={about}
                variant="filled"
                onChange={this.handleChange('about')}
                margin="normal"
                />
                <FormHelperText>Will this be your tech stock watchlist?</FormHelperText>
                <br/>
                <br/>
                </form>
            </DialogContent>
            <DialogActions>
            <Button 
            color="primary"
            variant="raised"
            onClick={this.handleSubmit}
            >
            Create Bucket
            </Button>
        </DialogActions>
        </Dialog>
    </Fragment>  
    }
}
export withStyles(styles)(YourComponent);

How would I be able to apply these styles to my code below?我如何能够将这些 styles 应用到下面的代码中? Thank you for assistance.谢谢你的帮助。

TL;DR: You are trying to use a React Hook const classes = useStyles; TL;DR:您正在尝试使用 React Hook const classes = useStyles; within a class component.在 class 组件内。 Also, useStyles is a function, not an object.此外, useStyles是 function,而不是 object。

NL;PR: Hooks only work on functional components ( useStyles() hooks are obtained via makeStyles() instead of withStyles() ). NL;PR:挂钩仅适用于功能组件( useStyles()挂钩是通过makeStyles()而不是withStyles()获得的)。 You apply HOC withStyles(YourComponent) to your class component not your styles, so you can access const {classes, ...rest} = this.props;您将 HOC withStyles(YourComponent)应用于您的 class 组件而不是您的 styles,因此您可以访问const {classes, ...rest} = this.props; in the render() method.render()方法中。

It should look like this:它应该如下所示:

const styles = theme => ({
    formControl: {
      width: 500
    },
    selectEmpty: {
      marginTop: theme.spacing(2),
    },});

class YourComponent extends PureComponent {
//...
 render(){
    const {classes, ...rest} = this.props;
// now your can access classes.formControl ...
 }
}

export withStyles(styles)(YourComponent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM