Reactjs 将类组件转换为功能组件 [英] Reactjs convert class components to functional components

查看:44
本文介绍了Reactjs 将类组件转换为功能组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 React 新手.我想在我的项目中使用表单步进器.我找到了一个库,但在该库中使用了类组件.我有点混淆将类组件转换为功能组件.我对反应的了解很短.

I am new in react. I want to use form stepper in my project. I found one library but in that library using class components. I am little confuse convert class components to functional components. I have short knowledge of 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) => {


  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)

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

How can I convert below code in functional components

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 });
  }


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

Please suggest me how to convert class components to functional components.

推荐答案

检查下面的代码:

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)

这篇关于Reactjs 将类组件转换为功能组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆