Search code examples
reactjsreact-hooksreact-functional-component

How to rewrite class component to React Functional?


I am new to React and I am learning the basics, but right now I need to rewrite a Class component in order to use React Hooks. So I guess I need to rewrite it to a functional component.

I already tried changing some of the things but in the end everything breaks and I will get a 'props not defined' error.

This is the code:

class Main extends React.Component {
  render() {
    let close = (
      <div
        className="close"
        onClick={() => {
          this.props.onCloseArticle()
        }}
      ></div>
    )

    return (
      <div
        ref={this.props.setWrapperRef}
        id="main"
        style={this.props.timeout ? { display: 'flex' } : { display: 'none' }}
      >
        <article
          id="vision"
          className={`${this.props.article === 'vision' ? 'active' : ''} ${
            this.props.articleTimeout ? 'timeout' : ''
          }`}
          style={{ display: 'none' }}
        >
          <h2 className="major">Vision</h2>
          <span className="image main">
            <img src={pic01} alt="" />
          </span>
          <p>
            Adipiscing magna sed dolor elit. Praesent eleifend dignissim arcu,
            at eleifend sapien imperdiet ac. Aliquam erat volutpat. Praesent
            urna nisi, fringila lorem et vehicula lacinia quam. Integer
            sollicitudin mauris nec lorem luctus ultrices.
          </p>
          <p>
            Nullam et orci eu lorem consequat tincidunt vivamus et sagittis
            libero. Mauris aliquet magna magna sed nunc rhoncus pharetra.
            Pellentesque condimentum sem. In efficitur ligula tate urna.
            Maecenas laoreet massa vel lacinia pellentesque lorem ipsum dolor.
            Nullam et orci eu lorem consequat tincidunt. Vivamus et sagittis
            libero. Mauris aliquet magna magna sed nunc rhoncus amet feugiat
            tempus.
          </p>
          {close}
        </article>
      </div>
    )
  }
}

Main.propTypes = {
  route: PropTypes.object,
  article: PropTypes.string,
  articleTimeout: PropTypes.bool,
  onCloseArticle: PropTypes.func,
  timeout: PropTypes.bool,
  setWrapperRef: PropTypes.func.isRequired,
}

export default Main

What I did is changing class main to const Main = () => {, remove the render() but after that I am confused..


Solution

  • This should do the work

    1. Replace class by const
    2. Remove the render lifecycle method used in class components
    3. Add the props in the parameter of the function
    4. Remove all the this
    const Main = (props) => {
        let close = (
          <div
            className="close"
            onClick={() => {
              props.onCloseArticle()
            }}
          ></div>
        )
    
        return (
          <div
            ref={props.setWrapperRef}
            id="main"
            style={props.timeout ? { display: 'flex' } : { display: 'none' }}
          >
            <article
              id="vision"
              className={`${props.article === 'vision' ? 'active' : ''} ${
                props.articleTimeout ? 'timeout' : ''
              }`}
              style={{ display: 'none' }}
            >
              <h2 className="major">Vision</h2>
              <span className="image main">
                <img src={pic01} alt="" />
              </span>
              <p>
                Adipiscing magna sed dolor elit. Praesent eleifend dignissim arcu,
                at eleifend sapien imperdiet ac. Aliquam erat volutpat. Praesent
                urna nisi, fringila lorem et vehicula lacinia quam. Integer
                sollicitudin mauris nec lorem luctus ultrices.
              </p>
              <p>
                Nullam et orci eu lorem consequat tincidunt vivamus et sagittis
                libero. Mauris aliquet magna magna sed nunc rhoncus pharetra.
                Pellentesque condimentum sem. In efficitur ligula tate urna.
                Maecenas laoreet massa vel lacinia pellentesque lorem ipsum dolor.
                Nullam et orci eu lorem consequat tincidunt. Vivamus et sagittis
                libero. Mauris aliquet magna magna sed nunc rhoncus amet feugiat
                tempus.
              </p>
              {close}
            </article>
          </div>
        )
    }