简体   繁体   English

React.js + Flow:将功能组件转换为基于类的组件

[英]React.js + Flow: Convert functional component into class based component

I have a functional component that I want to convert into a class based component, because it got too large. 我有一个要转换为基于类的组件的功能组件,因为它太大了。 I have a problem with converting the mapStateToProps function, can't really figure out how to fetch the user data. 我在转换mapStateToProps函数时遇到问题,无法真正弄清楚如何获取用户数据。

I'm using react-redux and Flow. 我正在使用react-redux和Flow。

import { connect } from 'react-redux'
import moment from 'moment'
import 'moment-timezone/builds/moment-timezone-with-data-2010-2020'
import React, { Component } from 'react'

import styles from './contact-user.scss'
import { CSSModules } from 'app/lib/css-modules'

const applyCSS = CSSModules(styles)

export const mapStateToProps = ({
  user: { data: {
    birthDate,
    city,
    firstName,
    lastName,
    adresses,
    timeZone } }
}) => ({
  birthDate: moment(birthDate).format('L'),
  city,
  firstName,
  lastName,
  adresses,
  time: timeZone ? moment().tz(timeZone).format('h:mm A z') : ''
})

export const ContactUser = connect(mapStateToProps)(applyCSS(({
  adresses,
  time
}) => {
  const addressDetails = () => {
    ...
  }

  const sortAddresses = (a, b) => {
    ...
  }

  const handleButtonClick = (phoneType) => {
    ...
  }

  return (
    <div>
      <h1>Contact the User</h1>
      <div className='text-center'>
        {
          Object.keys(adresses).length
          ? <MyComponent
              data={adresses.sort(sortAddresses)}
            />
          : <h2>No Adresses</h2>
        }
      </div>
      {time}
      ...
    </div>
  )
}))

I want to turn the above code into something like this: 我想把上面的代码变成这样:

// ... imports

import type { ActionCreator } from 'redux'
import type { Participants$PhonesModel } from '<coaching>/types/participants'

type State = {
  ContactUser: {
    birthDate: moment(birthDate).format('L'),
    city,
    firstName,
    lastName,
    adresses,
    time
  },
  ...
}

type Props = {
  // ... props
} & State

export class ContactUser extends Component {
  props: Props;

  // methods, etc.

  render () {
    return (
      <div>
        ...
      </div>
    )
  }
}

const mapStateToProps = ({state: State}) => {
  return {
    ...
  }
}

const mapDispatchToProps = { markPhoneAsInvalid, markPhoneAsValid }

export default connect(mapStateToProps, mapDispatchToProps)(ContactUser)

My main problem is that I can't figure out how to get the data for a user to the component via mapStateToProps . 我的主要问题是我无法弄清楚如何通过mapStateToProps将用户的数据获取到组件。

mapStateToProps implementation is the same, I must say. 我必须说mapStateToProps实现是相同的。 In any case: 任何状况之下:

class ContactUser extends Component {
  render() {
    ...
  }
}

const mapStateToProps = (state) => {
  const {
    birthDate,
    city,
    firstName,
    lastName,
    addresses,
    timeZone,
  } = state.user.data

  return {
    birthDate: moment(birthDate).format('L'),
    city,
    firstName,
    lastName,
    adresses,
    time: timeZone ? moment().tz(timeZone).format('h:mm A z') : ''
  }
}

const mapDispatchToProps = () => { ... }

export default connect(
  mapStateToProps,
  mapDispatchToProps,
)(ContactUser)

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

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