Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,91 @@ ConnectedRelapseButton.propTypes = {
}
```

## Hey What About Typescript?

Typescript Indeed... but what is it? Typescript is a superset of JavaScript that provides optional static typing, classes and interfaces. It helps as you develop by adding a richer environment to your IDE for spotting typos and coding errors.

First, let's add the dependencies.

`$ yarn add @types/react @types/react-dom typescript ts-loader source-map-loader`

Next let's add a **TypeScript** configuration file.

**tsconfig.json**

```
{
"compilerOptions": {
"outDir": "./dist/",
"sourceMap": true,
"noImplicitAny": true,
"module": "commonjs",
"target": "es6",
"jsx": "react"
}
}
```

And update our webpack config.

```
const HtmlWebPackPlugin = require("html-webpack-plugin");

module.exports = {
entry: "./src/index.js",

devtool: "source-map",

module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.ts(x?)$/,
exclude: /node_modules/,
use: [
{
loader: "ts-loader"
}
]
},
{
enforce: "pre",
test: /\.js$/,
loader: "source-map-loader"
},
{
test: /\.html$/,
use: [
{
loader: "html-loader"
}
]
}
]
},
externals: {
"react": "React",
"react-dom": "ReactDOM"
},
plugins: [
new HtmlWebPackPlugin({
template: "./src/index.html",
filename: "./index.html"
})
]
};
```





## Finishing Up

So, there it is... our sobriety/relapse tracker. I hope this tutorial didn't drive you to drink, but if it did feel safe in knowing that with this app, you're in control.
Expand Down
7 changes: 7 additions & 0 deletions sample-react-redux-tutorial/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"@babel/core": "^7.6.0",
"@babel/preset-env": "^7.6.0",
"@babel/preset-react": "^7.0.0",
"@types/react": "^16.9.9",
"@types/react-dom": "^16.9.2",
"babel-loader": "^8.0.6",
"html-loader": "^0.5.5",
"html-webpack-plugin": "^3.2.0",
Expand All @@ -25,5 +27,10 @@
"scripts": {
"build": "webpack --mode production",
"start": "webpack-dev-server --mode development"
},
"dependencies": {
"source-map-loader": "^0.2.4",
"ts-loader": "^6.2.0",
"typescript": "^3.6.4"
}
}
2 changes: 1 addition & 1 deletion sample-react-redux-tutorial/src/components/App.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'

import DatePicker from './DatePicker.jsx';
import DatePicker from './DatePicker.tsx';
import RelapseList from './RelapseList.jsx';
import RelapseButton from './RelapseButton.jsx';
import TimeDisplay from './TimeDisplay.jsx';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@ import React from 'react';
import { connect } from 'react-redux';
import { updateTimeSober } from '../actions/index';

function mapDispatchToProps(dispatch) {
function mapDispatchToProps(dispatch: any) {
return {
dispatchUpdateSobrietyDate: timeSober => dispatch(updateTimeSober(timeSober)),
dispatchUpdateSobrietyDate: (timeSober: string) => dispatch(updateTimeSober(timeSober)),
};
};

class ConnectedDatePicker extends React.Component {
interface DatePickerProps {
dispatchUpdateSobrietyDate: any;
};

class ConnectedDatePicker extends React.Component<DatePickerProps, {}> {
constructor() {
super();

this.handleChange = this.handleChange.bind(this);
}

handleChange(el) {
handleChange(el: any) {
el.preventDefault();
const date = new Date(el.target.value).toUTCString();
this.props.dispatchUpdateSobrietyDate(date);
Expand All @@ -27,10 +31,6 @@ class ConnectedDatePicker extends React.Component {
}
};

ConnectedDatePicker.propTypes = {
dispatchUpdateSobrietyDate: PropTypes.func.isRequired,
};

const DatePicker = connect(null, mapDispatchToProps)(ConnectedDatePicker);

export default DatePicker;
10 changes: 10 additions & 0 deletions sample-react-redux-tutorial/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"outDir": "./dist/",
"sourceMap": true,
"noImplicitAny": true,
"module": "commonjs",
"target": "es6",
"jsx": "react"
}
}
21 changes: 21 additions & 0 deletions sample-react-redux-tutorial/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ const HtmlWebPackPlugin = require("html-webpack-plugin");

module.exports = {
entry: "./src/index.js",

devtool: "source-map",

module: {
rules: [
{
Expand All @@ -11,6 +14,20 @@ module.exports = {
loader: "babel-loader"
}
},
{
test: /\.ts(x?)$/,
exclude: /node_modules/,
use: [
{
loader: "ts-loader"
}
]
},
{
enforce: "pre",
test: /\.js$/,
loader: "source-map-loader"
},
{
test: /\.html$/,
use: [
Expand All @@ -21,6 +38,10 @@ module.exports = {
}
]
},
externals: {
"react": "React",
"react-dom": "ReactDOM"
},
plugins: [
new HtmlWebPackPlugin({
template: "./src/index.html",
Expand Down
Loading