Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React component without extend React Component class

Tags:

reactjs

I see in some source code, the author wrote a component like this:

import React from 'react';

export const Login = () => (
  <div>
    <h4>Hello World</h4>
  </div>
);

export default Login;

The thing I don't know is:

  1. How react understand this is a react component by only using import
  2. How can I add another callback method, such as viewDidMount ... I have added in code block but it compiles fail.

thanks

like image 693
Trần Kim Dự Avatar asked Apr 30 '17 07:04

Trần Kim Dự


People also ask

Do you need to extend React component?

If you want to write a component that takes advantage of the lifecycle methods you will need to extend the default Component class. The Component class contains the lifecycle methods and should not be confused with other generic react components, such as functional components or any component you may write.

Is React class component deprecated?

Yes, React class components will fade away in the future. If you want to embrace modern React, then you should use function components with hooks. That's why you will find most tutorials out there teaching modern React and no class components anymore.

Can a functional component extend a class component?

Whenever a class component is created, it is necessary to extend the component, however, if a functional component is created, it is not necessary to extend the component. With functional components, we can use props directly.

Can I pass a component as a prop?

You can pass a component as props in React by using the built-in children prop. All elements you pass between the opening and closing tags of a component get assigned to the children prop.


2 Answers

This is functional stateless component. It is for simple components.

You also can add component property types like this:

export const Login = () => (
  <div>
    <h4>Hello World</h4>
  </div>
);

Login.propTypes = {
  username: React.PropTypes.string.isRequired,
}

You can add callback like this:

// this place to define callback is more effective 
const onClick = e => (...)

const Login = props => {
  // or define here if you need access to props in onClick
  const onClick = e => (...)

  return <button onClick={onClick}>Submit</button>
}
like image 139
user394010 Avatar answered Nov 26 '22 22:11

user394010


React "knows" that the code you wrote is a React Component because of transpilation. Transpilation is a process that occurs during build time where your code is changed from the code you wrote into something else.

In the case of React and JSX, your code turns into

export const Login = () => (
  React.createElement('div', {},
    React.createElement('h4', {}, 'Hello World')
  );
);

The angle brackets (<) are syntactic sugar for React.createElement and people use the angle brackets because they are simpler to use and type.

like image 24
Merlin -they-them- Avatar answered Nov 26 '22 20:11

Merlin -they-them-