React.js将类组件转换为功能组件

时间:2019-12-05 13:04:53

标签: reactjs react-redux

我是新来的人。我想在项目中使用表单步进器。我找到了一个库,但是在该库中使用类组件。我很少混淆将类组件转换为功能组件。我对反应一无所知。

import {connect} from 'react-redux'
import { Link } from 'react-router-dom'
import PropTypes from 'prop-types'
import Stepper from 'react-stepper-horizontal'
import { Card } from 'reactstrap'
import GeneralForm from './../../components/con/GeneralForm'
import PersonalDetailsForm from './../../components/con/PersonalDetailsForm'
import NomineeDetailsForm from './../../components/con/NomineeDetailsForm'

const ConCreate = (props) => {


  constructor(props) {
    super(props);
    this.nextPage = this.nextPage.bind(this);
    this.previousPage = this.previousPage.bind(this);
    this.state = {
      page: 0,
      steps: [
        {title: 'Visitor Details'},
        {title: 'Personal Details'},
        {title: 'Nominee Details'}
      ]
    };
  }

  nextPage() {
    this.setState({ page: this.state.page + 1 });
  }

  previousPage() {
    this.setState({ page: this.state.page - 1 });
  }




  return (

    <Card>
       <Stepper steps={ steps } activeStep={ page } />
         {page === 0 && <GeneralForm onSubmit={nextPage} />}
         {page === 1 && (
       <PersonalDetailsForm
       previousPage={previousPage}
       onSubmit={nextPage}
       />
       )}
       {page === 2 && (
       <NomineeDetailsForm
       previousPage={previousPage}
       onSubmit={onSubmit}
       />
       )}
     </Card>
  )
}

ConCreate.propTypes = {

}

const mapDispatchToProps = {}

function mapStateToProps (state) {

}

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(ConCreate)

如何在功能组件中转换以下代码

constructor(props) {
    super(props);
    this.nextPage = this.nextPage.bind(this);
    this.previousPage = this.previousPage.bind(this);
    this.state = {
      page: 0,
      steps: [
        {title: 'Visitor Details'},
        {title: 'Personal Details'},
        {title: 'Nominee Details'}
      ]
    };
  }

  nextPage() {
    this.setState({ page: this.state.page + 1 });
  }

  previousPage() {
    this.setState({ page: this.state.page - 1 });
  }


请建议我如何将类组件转换为功能组件。

3 个答案:

答案 0 :(得分:1)

检查以下代码:

import React, {useState} from 'react'
import {connect} from 'react-redux'
import { Link } from 'react-router-dom'
import PropTypes from 'prop-types'
import Stepper from 'react-stepper-horizontal'
import { Card } from 'reactstrap'

import GeneralForm from './../../components/con/GeneralForm'
import PersonalDetailsForm from './../../components/con/PersonalDetailsForm'
import NomineeDetailsForm from './../../components/con/NomineeDetailsForm'

const ConCreate = (props) => {

  const initStepsValue = [
    {title: 'Visitor Details'},
    {title: 'Personal Details'},
    {title: 'Nominee Details'}
  ]

  const [page, setPage] = useState(0)
  const [steps, setSteps] = useState(initStepValue)

  const nextPage = () => setPage(page + 1) 
  const prevPage = () => previousPage(page - 1)


  return (
    <Card>
       <Stepper steps={ steps } activeStep={ page } />
         {page === 0 && <GeneralForm onSubmit={nextPage} />}
         {page === 1 && (
           <PersonalDetailsForm
             previousPage={previousPage}
             onSubmit={nextPage}
           />
         )}
         {page === 2 && (
           <NomineeDetailsForm
             previousPage={previousPage}
             onSubmit={onSubmit}
           />
         )}
     </Card>
  )
}

ConCreate.propTypes = {}

const mapDispatchToProps = {}

const mapStateToProps = state => ({})

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(ConCreate)

答案 1 :(得分:0)

功能组件不像类那样使用状态。您可以将useState方法与功能组件一起使用。但是要回答你的问题。

  class MyClassComponent extends Component {
     render() {
      return(
       // code
    )
   }      
 }
    export default MyClassComponent;

这是一个功能组件

function MyFunctionalComponent() {
    return(
    //code
  )
}
export default MyFunctionalComponent;

答案 2 :(得分:0)

用此代码替换您的构造函数和函数,基本上不需要构造函数和“ this”。

update