React course: change state

Hello,

I’m in the react course at the piece where we create a random percentage change of the price but I get a error:
" Type annotations can only be used in TypeScript files"

This is my code:

import React, { Component } from 'react'
import './Coin.css';
import PropTypes from "prop-types";

export default class  Coin extends Component {
    constructor(props){
        super(props);
        this.state = {
            price: this.props.price
        }
    }
    componentDidMount(){
        const callback = () => {
            // set the state to a new random value
            const randomPercentage = 0.995 + Math.random() * 0.01;
            // not allowed to change the value of the state
            this.setState(function(oldState){
                return(
                    price: oldState.price * randomPercentage
                );
            });
        }
        setInterval(callback, 1000)
    }


  render() {
    return(
        <tr className='coin-row'>
          <td>{this.props.name}</td>
          <td>{this.props.ticker}</td>
          <td>${this.state.price}</td>
        </tr>
      );
  }
}

Coin.propTypes ={
    name: PropTypes.string.isRequired,
    ticker: PropTypes.string.isRequired,
    price: PropTypes.number.isRequired
}

Can you help me?

Thanks!

One solution could be to modify settings of VS Code to
“javascript.validate.enable”: false

1 Like

Hi @Gry,

Thanks! Now the error in my VS is gone!
But when I start my localhost I still have an error. Namely:

Can you help me with this one as well please?

Thanks in advance!

Try using brackets on code at line 18 and line 20, change

return(

                    price: oldState.price * randomPercentage

                );

to

return{

                    price: oldState.price * randomPercentage

                };
1 Like

Thanks! Everything works now!

1 Like