How to convert the react function into class?

吃可爱长大的小学妹 提交于 2021-01-29 07:36:12

问题


I'm used the material-UI to develop the react.js frontend. But when I'm gonna develop it furthermore(to implement the crud operations), it's hard to code, since every tutorial coded by using class, not a function. So, I tried to convert the code into the class. But I failed to do that properly.

So I just ask you guys to help me to convert the below code into the class. I would like to appreciate your kindness. Thank you.

import React from "react";
import ReactDom from 'react-dom';
import Button from '@material-ui/core/Button';
import { Container, Row, Col, Form } from "react-bootstrap";
import TextField from '@material-ui/core/TextField';
import { makeStyles, withStyles } from '@material-ui/core/styles';

const useStyles = makeStyles((theme) => ({
    root: {
        '& > *': {
            margin: theme.spacing(1),
            width: '25ch',
        },
    },
    button: {
        marginRight: theme.spacing(1),
    },
}));

export default function Step2(props) {

    const classes = useStyles();

    return (
        <Container style={{marginRight: 700}}>
            <Row
                style={{ marginTop: "30px" }}
                className="h-100 justify-content-center align-items-center"
            >
                <Col md={{ span: 6 }} className="text-center my-auto">
                    <h3 style={{ marginBottom: "1rem" }}>New Vehicle Details</h3>
                    <Form>
                        <form className={classes.root} noValidate autoComplete="off">
                            <TextField id="standard-basic" label="Vehicle No." required/>
                        </form>
                        <form className={classes.root} noValidate autoComplete="off">
                            <TextField id="standard-basic" label="Registered Year." required/>
                        </form>
                        <form className={classes.root} noValidate autoComplete="off">
                            <TextField id="standard-basic" label="Capacity" required/>
                        </form>
                        <form className={classes.root} noValidate autoComplete="off">
                            <TextField id="standard-basic" label="Type" required/>
                        </form>
                        <Button
                            variant="contained"
                            color="primary"
                            //onClick={handleNext}
                            className={classes.button}
                        >
                            Add
                        </Button>
                        <Button
                            variant="contained"
                            color="secondry"
                            // onClick={handleNext}
                            className={classes.button}
                        >
                            Cancel
                        </Button>
                    </Form>
                </Col>
            </Row>
        </Container>
    );
}

回答1:


you are now getting props in parameters of this function just change the function to class.

export default class Step2 extends React.Component {
}

and use props like this.props instead of props



来源:https://stackoverflow.com/questions/64058857/how-to-convert-the-react-function-into-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!