dimanche 3 avril 2022

TypeError: Cannot read properties of undefined (reading 'state')

On my react application, i am trying to add a section where it shows facts randomly for 5 second intervals. However i am stumbling on the error "TypeError: Cannot read properties of undefined (reading 'state')"

import React from "react";
import "./leftbar.css"


//class for producing facts 

class factSlides {
  constructor(){
    super();
    this.state = {
      abcFacts: ''
    };
  }
}

const factArr = ["a.", "b", "c", "d", "e"]



    setInterval(() => {
      this.setState({abcFacts:factArr[Math.floor(Math.random()*this.factArr.length)]});


    }, 5000);

Below is the code snippet of how it is exported

export default function Leftbar() {
  return (
      <div className="leftbar">
        <div className="leftbarWrapper">
          <div className="leftBarTitle">
            Did you know?
          </div>
          <div className="space">
            &nbsp;
          </div>
          <div className="fact">
          {this.state.abcFacts}
          </div>

css file

.fact{
    color: #3D1959;
    font-weight: lighter;
    justify-content: space-between;
    align-items: center;
}



Aucun commentaire:

Enregistrer un commentaire