Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the <{}> syntax after extends Component?

I started a new project today using React Native 0.51.0 and noticed that the class syntax for the default project file had something new added, the <{}> syntax after extends Component:

export default class App extends Component<{}> {
   ...
}

I tried doing research but most search engines ignore special characters even with exact string matching, so trying to find out what this syntax is has proved to be difficult. I did some testing and was able to figure out that this change appeared in v0.49.0. The release notes make no mention of what this added syntax does though.

A lot of vague keyword searching and reading leads me to believe that this may be syntax related to TypeScript, but being unfamiliar with the language, I'm at a loss as to how to search and find out more about the syntax without knowing what the proper term for it is. Could anyone tell me what the name of the syntax and what it does? Specifically with regards to React Native.

like image 783
Michael Cheng Avatar asked Dec 17 '17 09:12

Michael Cheng


People also ask

What does extends React component do?

Implement Inheritance in React Using the extends keyword, you can allow the current component to access all the component's properties, including the function, and trigger it from the child component. This example creates one component called ParentClass.

What syntax do you use to create a component in React?

You will be using JSX syntax, which you can learn about in our How To Create Elements with JSX tutorial. You will also need a basic knowledge of JavaScript, which you can find in How To Code in JavaScript, along with a basic knowledge of HTML and CSS. A good resource for HTML and CSS is the Mozilla Developer Network.

What is the correct syntax for fragments in React?

Keyed Fragments The shorthand syntax does not accept key attributes. You need a key for mapping a collection to an array of fragments such as to create a description list. If you need to provide keys, you have to declare the fragments with the explicit <React. Fragment> syntax.

When you modify the state of a React component the component?

Changing the state Object To change a value in the state object, use the this. setState() method. When a value in the state object changes, the component will re-render, meaning that the output will change according to the new value(s).

What is the use of extends keyword in C++?

The extends keyword is used in class declarations or class expressions to create a class which is a child of another class.

What does extends mean in Java?

Definition and Usage The extends keyword extends a class (indicates that a class is inherited from another class). In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories:

How to extend a class component in react?

A class component must include the extends React.Component statement. This statement creates an inheritance to React.Component, and gives your component access to React.Component's functions. The component also requires a render () method, this method returns HTML.

What is the difference between class extends and inherited properties?

The class that extends the properties is the child class or the derived class which comes before the extends keyword, while the class from which the properties are inherited is the parent class or superclass or the base class, and this class comes after the extends keyword.


1 Answers

It is related to Flow typings for the props you will receive in the component. Component<{}> would mean that you don't expect the component to receive props.

With Flow and React.Component, you can define types for props and state (see React$Component type declaration for details).

Example from Flow documentation about React components

import * as React from 'react';

type Props = { /* ... */ };

type State = {
  count: number,
};

class MyComponent extends React.Component<Props, State> {
  state = {
    count: 0,
  };

  componentDidMount() {
    setInterval(() => {
      this.setState(prevState => ({
        count: prevState.count + 1,
      }));
    }, 1000);
  }

  render() {
    return <div>Count: {this.state.count}</div>;
  }
}

<MyComponent />;
like image 97
Fidan Hakaj Avatar answered Oct 12 '22 06:10

Fidan Hakaj