Skip to content

Commit 2a45ffc

Browse files
committed
state should be readonly always everywhere
1 parent 428da03 commit 2a45ffc

File tree

8 files changed

+36
-35
lines changed

8 files changed

+36
-35
lines changed

README.md

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,12 @@ export interface StatefulCounterProps {
226226
label: string;
227227
}
228228

229-
type State = {
230-
count: number;
231-
};
229+
interface State {
230+
readonly count: number;
231+
}
232232

233233
export class StatefulCounter extends React.Component<StatefulCounterProps, State> {
234-
state: State = {
234+
readonly state: State = {
235235
count: 0,
236236
};
237237

@@ -272,11 +272,11 @@ export interface StatefulCounterWithDefaultProps {
272272
}
273273

274274
interface DefaultProps {
275-
initialCount: number;
275+
readonly initialCount: number;
276276
}
277277

278278
interface State {
279-
count: number;
279+
readonly count: number;
280280
}
281281

282282
export const StatefulCounterWithDefault: React.ComponentClass<StatefulCounterWithDefaultProps> =
@@ -287,7 +287,7 @@ export const StatefulCounterWithDefault: React.ComponentClass<StatefulCounterWit
287287
initialCount: 0,
288288
};
289289

290-
state: State = {
290+
readonly state: State = {
291291
count: this.props.initialCount,
292292
};
293293

@@ -373,11 +373,11 @@ interface NameProviderProps {
373373
}
374374

375375
interface NameProviderState {
376-
name: string;
376+
readonly name: string;
377377
}
378378

379379
export class NameProvider extends React.Component<NameProviderProps, NameProviderState> {
380-
state = {
380+
readonly state: NameProviderState = {
381381
name: 'Piotr',
382382
};
383383

@@ -403,12 +403,12 @@ export interface MouseProviderProps {
403403
}
404404

405405
interface MouseProviderState {
406-
x: number;
407-
y: number;
406+
readonly x: number;
407+
readonly y: number;
408408
}
409409

410410
export class MouseProvider extends React.Component<MouseProviderProps, MouseProviderState> {
411-
state = { x: 0, y: 0 };
411+
readonly state: MouseProviderState = { x: 0, y: 0 };
412412

413413
handleMouseMove = (event: React.MouseEvent<HTMLDivElement>) => {
414414
this.setState({
@@ -463,14 +463,14 @@ export const withState = <P extends WrappedComponentProps>(
463463
initialCount?: number;
464464
}
465465
interface State {
466-
count: number;
466+
readonly count: number;
467467
}
468468

469469
return class WithState extends React.Component<Subtract<P, WrappedComponentProps> & Props, State> {
470470
// Enhance component name for debugging and React-Dev-Tools
471471
static displayName = `withState(${WrappedComponent.name})`;
472472

473-
state: State = {
473+
readonly state: State = {
474474
count: Number(this.props.initialCount) || 0,
475475
};
476476

@@ -532,13 +532,13 @@ export const withErrorBoundary = <P extends WrappedComponentProps>(
532532
) => {
533533
interface Props { }
534534
interface State {
535-
error: Error | null | undefined;
535+
readonly error: Error | null | undefined;
536536
}
537537

538538
return class WithErrorBoundary extends React.Component<Subtract<P, WrappedComponentProps> & Props, State> {
539539
static displayName = `withErrorBoundary(${WrappedComponent.name})`;
540540

541-
state: State = {
541+
readonly state: State = {
542542
error: undefined,
543543
};
544544

@@ -852,7 +852,7 @@ export type TodosState = {
852852
};
853853

854854
export type RootState = {
855-
todos: TodosState;
855+
readonly todos: TodosState;
856856
};
857857

858858
export const todosReducer = combineReducers<TodosState, TodosAction>({
@@ -1345,7 +1345,8 @@ configure({ adapter: new Adapter() });
13451345
// in webpack you need to add -> resolve: { alias: { '@src': PATH_TO_SRC } }
13461346
},
13471347
"outDir": "dist/", // target for compiled files
1348-
"allowSyntheticDefaultImports": true, // no errors on commonjs default import
1348+
"allowSyntheticDefaultImports": true, // no errors with commonjs modules interop
1349+
"esModuleInterop": true,
13491350
"allowJs": true, // include js files
13501351
"checkJs": true, // typecheck js files
13511352
"declaration": false, // don't emit declarations

playground/src/components/mouse-provider.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ export interface MouseProviderProps {
55
}
66

77
interface MouseProviderState {
8-
x: number;
9-
y: number;
8+
readonly x: number;
9+
readonly y: number;
1010
}
1111

1212
export class MouseProvider extends React.Component<MouseProviderProps, MouseProviderState> {
13-
state = { x: 0, y: 0 };
13+
readonly state: MouseProviderState = { x: 0, y: 0 };
1414

1515
handleMouseMove = (event: React.MouseEvent<HTMLDivElement>) => {
1616
this.setState({

playground/src/components/name-provider.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ interface NameProviderProps {
55
}
66

77
interface NameProviderState {
8-
name: string;
8+
readonly name: string;
99
}
1010

1111
export class NameProvider extends React.Component<NameProviderProps, NameProviderState> {
12-
state = {
12+
readonly state: NameProviderState = {
1313
name: 'Piotr',
1414
};
1515

playground/src/components/stateful-counter-with-default.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ export interface StatefulCounterWithDefaultProps {
66
}
77

88
interface DefaultProps {
9-
initialCount: number;
9+
readonly initialCount: number;
1010
}
1111

1212
interface State {
13-
count: number;
13+
readonly count: number;
1414
}
1515

1616
export const StatefulCounterWithDefault: React.ComponentClass<StatefulCounterWithDefaultProps> =
@@ -21,7 +21,7 @@ export const StatefulCounterWithDefault: React.ComponentClass<StatefulCounterWit
2121
initialCount: 0,
2222
};
2323

24-
state: State = {
24+
readonly state: State = {
2525
count: this.props.initialCount,
2626
};
2727

playground/src/components/stateful-counter.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ export interface StatefulCounterProps {
44
label: string;
55
}
66

7-
type State = {
8-
count: number;
9-
};
7+
interface State {
8+
readonly count: number;
9+
}
1010

1111
export class StatefulCounter extends React.Component<StatefulCounterProps, State> {
12-
state: State = {
12+
readonly state: State = {
1313
count: 0,
1414
};
1515

playground/src/hoc/with-error-boundary.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ export const withErrorBoundary = <P extends WrappedComponentProps>(
1212
) => {
1313
interface Props { }
1414
interface State {
15-
error: Error | null | undefined;
15+
readonly error: Error | null | undefined;
1616
}
1717

1818
return class WithErrorBoundary extends React.Component<Subtract<P, WrappedComponentProps> & Props, State> {
1919
static displayName = `withErrorBoundary(${WrappedComponent.name})`;
2020

21-
state: State = {
21+
readonly state: State = {
2222
error: undefined,
2323
};
2424

playground/src/hoc/with-state.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ export const withState = <P extends WrappedComponentProps>(
1515
initialCount?: number;
1616
}
1717
interface State {
18-
count: number;
18+
readonly count: number;
1919
}
2020

2121
return class WithState extends React.Component<Subtract<P, WrappedComponentProps> & Props, State> {
2222
// Enhance component name for debugging and React-Dev-Tools
2323
static displayName = `withState(${WrappedComponent.name})`;
2424

25-
state: State = {
25+
readonly state: State = {
2626
count: Number(this.props.initialCount) || 0,
2727
};
2828

playground/src/redux/todos/reducer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export type TodosState = {
1212
};
1313

1414
export type RootState = {
15-
todos: TodosState;
15+
readonly todos: TodosState;
1616
};
1717

1818
export const todosReducer = combineReducers<TodosState, TodosAction>({

0 commit comments

Comments
 (0)