0

I have created a property (age) in the state Object. However, I can not access this property in my code. I am getting an Error Message:

Property 'age' does not exist on type '{}

Here below is my code :

import * as React from 'react';
import styles from './ReactPnP.module.scss';
import myStyles from './myStyle.module.scss';
import { IReactPnPProps } from './IReactPnPProps';
import { escape } from '@microsoft/sp-lodash-subset';

import { Web } from "sp-pnp-js";
import pnp from "sp-pnp-js";


interface ISPVehicule {
  Title: string;
  Marque: string;
  Modele: string;
  Vitesse: string;
  Poid: string;
}


export default class ReactPnP extends React.Component<IReactPnPProps, {}> {

  private vehiculeList:  ISPVehicule[];
  private web;

  constructor(props)
  {
    super(props);
    this.state = { age: 0};
    this.web = new Web(this.props.contextUrl);

    var initialAge = this.state.age;
  ...

Do you know I am doing wrong?

2

Your code tell the TypeScript that the state object is empty object with no members. React.Component accepts two generic types - properties object and state object. You have to change this line

export default class ReactPnP extends React.Component<IReactPnPProps, {}> {

to

export default class ReactPnP extends React.Component<IReactPnPProps, any> {

or to some specific object like

interface IState {
  age: number;
}

export default class ReactPnP extends React.Component<IReactPnPProps, IState> {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.