Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Building Applications with React and Redux Putting it all Together Updating the Player, Counter and AddPlayerForm Components

page won't render

I'm using VSCode and I keep getting an error in my code as well as the console

In my AddPlayerForm.js file I get this error

'types' can only be used in a .ts file.ts(8010)

It appears over

static propTypes: {
      addPlayer: PropTypes.func.isRequired,
 };

THen in the console I recieve this error

Uncaught TypeError: _react.default.createContext is not a function

here is my AddPlayerForm.js file

import React, { Component, PropTypes } from 'react';

 export default class AddPlayerForm extends Component {
static propTypes: {
      addPlayer: PropTypes.func.isRequired,
 };

      state = {
          name: ''
      };

      onNameChange = (e) => {
        const name = e.target.value;
        this.setState({ name: name });
      };

      addPlayer = (e) => {
        if (e) e.preventDefault();
        this.props.addPlayer(this.state.name);
        this.setState({ name: '' });
      };

      render() {
        return (
          <div className="add-player-form">
            <form onSubmit={this.addPlayer}>
              <input
                type="text"
                value={this.state.name}
                onChange={this.onNameChange}
                placeholder="Player Name"
              />
              <input type="submit" value="Add Player" />
            </form>
          </div>
        );
      }
}

1 Answer

John Nguyen
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
John Nguyen
Full Stack JavaScript Techdegree Graduate 30,501 Points

To solve the error for 'types' can only be used in a .ts file.ts(8010), you can update the syntax to show:

static propTypes = {
  addPlayer: PropTypes.func.isRequired,
};

As for the other error: Uncaught TypeError: _react.default.createContext is not a function, it (most likely) has to do with your react-redux and redux versions.

I solved the issue by

1) downloading the project files(for this video) and copying the package.json file from the 'final' version of this video.

Package.json

{
  "name": "react-redux-course",
  "version": "1.0.0",
  "description": "React-Redux course for Treehouse",
  "main": "src/index.js",
  "scripts": {
    "prestart": "npm run lint",
    "start": "webpack-dev-server --progress --inline --hot",
    "lint": "eslint ./src/**/*.{js, jsx} --fix",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "react",
    "redux",
    "treehouse"
  ],
  "author": "Beau Palmquist",
  "license": "MIT",
  "dependencies": {
    "react": "^15.3.2",
    "react-dom": "^15.3.2",
    "react-redux": "^4.4.5",
    "redux": "^3.6.0"
  },
  "devDependencies": {
    "babel-core": "^6.13.2",
    "babel-eslint": "^6.1.2",
    "babel-loader": "^6.2.7",
    "babel-preset-es2015": "^6.13.2",
    "babel-preset-react": "^6.11.1",
    "babel-preset-stage-0": "^6.5.0",
    "eslint": "^3.3.1",
    "eslint-plugin-react": "^6.1.2",
    "react-hot-loader": "^1.3.0",
    "webpack": "^1.13.2",
    "webpack-dev-server": "^1.16.2"
  }
}

2) running npm i

And that should fix the issue.

If you are still running into issues, you'd want to check your code compared to the 'final' version of this video's code. I had to do this and I used diffchecker.com to compare my code to the 'final' version.