Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does parameter 'props' implicitly has an 'any' type?

I was trying to create a store with create-react-app-typescript. Everything was going fine.....however then this error occurred "Parameter 'props' implicitly has an 'any' type" and I found a solution for this in the internet...it suggested to create a interface for props. But I really don't know what that object has so i can't even do that...

Any help is appreciated! (I am new to TypeScript)

import * as React from 'react';
import { connect } from "react-redux";
import mapStateToProps from "./utilities/mapStateToProp";

class App extends React.Component {
  constructor(props){
    super(props);
  }

  public render() {
    return (
      <div className="App">
        <h1>Go Type React</h1>
      </div>
    );
  }
}

export default connect(mapStateToProps)(App);
like image 887
Evading Shadows Avatar asked Oct 10 '18 07:10

Evading Shadows


People also ask

Are Props just parameters?

Props are also how you pass data from one component to another, as parameters.

Why are props read only?

Props are Read-Only Such functions are called “pure” because they do not attempt to change their inputs, and always return the same result for the same inputs.

What is the difference between state and props and why it is not recommended to update state directly in React native?

props are passed via component properties, they're not reactive. State are variables that react will react , updating the UI when values changes. I think what is meant by "props should not be changed" is that they should not be changed by children components.

Are Props the same as parameters?

Props : parameters. Like functions, components are meant to be re-usable and do only one thing. Props are the values that are passed in to components to be processed (aka displayed, used to decided something, etc) inside the component. Unlike parameters, props should not be changed.


2 Answers

You should always declare your props and state objects with generics on the Component class. Restrain from using the any keyword whenever possible.

Tip: A modern IDE allows you to F12 and inspect the definition file, that is a great help if your new to TypeScript. You can also read the React definition on the DT GitHub repo, the React.Component are defined here

type AppProps = {}
type AppState = {}
class App extends React.Component<AppProps, AppState> {
    constructor(props: AppProps) {
        super(props);
        //this.props will already be of type AppProps. 
        //Only the constructor props are any
    }

    public render() {
        return (
            <div className="App">
                <h1>Go Type React</h1>
            </div>
        );
    }
}
like image 107
Per Svensson Avatar answered Nov 13 '22 03:11

Per Svensson


For function components:

import { FunctionComponent } from "react";

// This allows you to use `props.children`
export const App: FunctionComponent = (props) => {}

// Or if you have custom props you can define them
export const App: FunctionComponent<{key: String}> = (props) => {}
like image 33
thisismydesign Avatar answered Nov 13 '22 03:11

thisismydesign