E
E
ElizarBatin2018-10-02 16:28:29
OOP
ElizarBatin, 2018-10-02 16:28:29

OOP + React + typescript how to create components correctly?

Are there any examples of view components that have been written with typescript using OOP?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-10-02
@ElizarBatin

I write like this:

import * as React from 'react';
import { connect, DispatchProp } from 'react-redux';
import { connectedPropSelector } from './selectors';

interface OwnProps {
  ownProp: string,
}

interface ConnectedProps {
  connectedProp: string,
}

type Props = OwnProps & ConnectedProps & DispatchProp<any>;

interface State {
  someKey: string,
}

class Example extends React.Component<Props, State> {
  state = {
    someKey: 'someValue',
  };
  
  render() {
    const { ownProp, connectedProp } = this.props;

    return ( /* ... */ );
  }
}

const mapStateToProps = state => ({
  connectedProp: connectedPropSelector(state),
});

export default connect(mapStateToProps)(Example);

Official documentation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question