2

I am new to typescript and am wondering how to pass the prop from my class component to a parent class.

My child class looks like this:

import React from "react";

type Props = {
startTimeInSeconds: number;
}

type State = {
timeRemainingInSeconds: number;
}


export default class Child extends React.Component<Props, State>{
    private timer: any;

constructor(props: Props) {
  super(props);
  this.state = {
    timeRemainingInSeconds: props.startTimeInSeconds
  };
}

decrementTimeRemaining = () => {
  if (this.state.timeRemainingInSeconds > 0) {
    this.setState({
      timeRemainingInSeconds: this.state.timeRemainingInSeconds - 1
    });
  } else {
    clearInterval(this.timer!);
  }
};

componentDidMount() {
  this.timer = setInterval(() => {
    this.decrementTimeRemaining();
  }, 1000);
}

render() {
  return (
    <div className="countdown-timer">
      <div className="countdown-timer__circle">
        <svg>
          <circle
            r="24"
            cx="26"
            cy="26"
            style={{
              animation: `countdown-animation ${this.props
                .startTimeInSeconds}s linear`
            }}
          />
        </svg>
      </div>
      <div className="countdown-timer__text">
        {this.state.timeRemainingInSeconds}s
      </div>
    </div>
  );
}
}

I wanted to call this child component in my parent component but I don't get how it is working. I tried to call the child within the parent with

<Child startTimeInSeconds = { this.startTimeInSeconds } />

but it throws an error.

A (simplified) parent component looks like this:

import '../css/App.css';
import * as React from "react";
import Child from "./ChildComponent"

function App(props) {
  return (
    <>
    <div>
        <Child startTimeInSeconds = { this.startTimeInSeconds } />
    </div>
    </>
  );
}

export default App;
9
  • 1
    What is the error that it throws? Commented Jun 18, 2021 at 12:21
  • Would be nice to see a parent of Child Component, since I suspect you pass the prop in the wrong way. Child component seems fine Commented Jun 18, 2021 at 12:24
  • Please share parente component code and error details Commented Jun 18, 2021 at 12:28
  • you can;t use this in functional components. Commented Jun 18, 2021 at 12:35
  • 1
    That's fine. You have to defined startTimeInSeconds in App component. Commented Jun 18, 2021 at 12:59

1 Answer 1

2

Your Child component is fine, but you are passing incorrect props to it.

In App component you need to define startTimeInSeconds variable>

You parent component should look like this -

import '../css/App.css';
import * as React from "react";
import Child from "./ChildComponent"

function App(props) {
  const startTimeInSeconds = 0; //or any number
  return (
    <>
    <div>
        <Child startTimeInSeconds = { startTimeInSeconds } />
    </div>
    </>
  );
}

export default App;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.