U
U
Username2018-07-04 22:17:42
React
Username, 2018-07-04 22:17:42

What is the difference between writing React components?

Hello!
Can you tell in more detail about the 2nd type of writing code, what are its advantages? Recommend, please, articles for studying 2 types of syntax.
1 - classic view from off site

class Reservation extends React.Component {
  constructor(props) {
    super(props);
    this.handleInputChange = this.handleInputChange.bind(this);
  }

  handleInputChange(event) {
    const target = event.target;
    const value = target.type === 'checkbox' ? target.checked : target.value;
    const name = target.name;

    this.setState({
      [name]: value
    });
  }

  render() {
    return (
      <form>
        <label>
          Number of guests:
          <input
            name="numberOfGuests"
            type="number"
            value={this.state.numberOfGuests}
            onChange={this.handleInputChange} />
        </label>
      </form>
    );
  }
}

2 - view
import React from 'react';
import { string } from 'prop-types';
const Link = ({ title, url }) => <a href={url}>{title}</a>;
Link.propTypes = {
  title: string.isRequired,
  url: string.isRequired
};
export default Link;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2018-07-04
@dalv_happy

In the first case, the component is completely inherited from the Component class, which means that you can use lifecycle methods, write your own methods, the component will be able to store its local state. Those. some logic will be described inside the component.
In the second option, the component is only responsible for displaying, i.e. all of the above operations cannot be described inside this component.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question