Skip to content

Commit 578966c

Browse files
Remove deprecated class property reducer (#10)
For the upcoming v1.0.0-rc1, we want to remove support for the deprecated class property reducer.
1 parent ef07fe9 commit 578966c

File tree

4 files changed

+12
-58
lines changed

4 files changed

+12
-58
lines changed

__tests__/ReComponent-test.js

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -106,36 +106,4 @@ describe("ReComponent", () => {
106106
process.env.NODE_ENV = originalNodeEnv;
107107
}
108108
});
109-
110-
it("warns when the reducer is not static", () => {
111-
let originalWarn = console.warn;
112-
try {
113-
console.warn = jest.fn();
114-
115-
let click;
116-
class ClassPropertyReducer extends ReComponent {
117-
constructor() {
118-
super();
119-
click = () => this.send({ type: "CLICK" });
120-
}
121-
122-
reducer(action, state) {
123-
return NoUpdate();
124-
}
125-
126-
render() {
127-
return null;
128-
}
129-
}
130-
131-
ReactDOM.render(<ClassPropertyReducer />, container);
132-
expect(() => click()).not.toThrowError();
133-
134-
expect(console.warn).toHaveBeenCalledWith(
135-
"ClassPropertyReducer(...): Class property `reducer` methods are deprecated. Please upgrade to `static` reducers instead: https://github.com/philipp-spiess/react-recomponent#why-is-the-reducer-static"
136-
);
137-
} finally {
138-
console.warn = originalWarn;
139-
}
140-
});
141109
});

__tests__/__snapshots__/ReComponent-test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
exports[`ReComponent disables \`setState\` 1`] = `"Example(...): Calls to \`setState\` are not allowed. Please use the \`reducer\` method to update the component state"`;
44

5-
exports[`ReComponent errors when no \`reducer\` method is defined 1`] = `"Example(...): No \`reducer\` method found on the returned component instance: did you define a reducer?"`;
5+
exports[`ReComponent errors when no \`reducer\` method is defined 1`] = `"Example(...): No static \`reducer\` method found on the returned component instance: did you define a reducer?"`;

src/re.js

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,11 @@ export function Re(Component) {
1313
if (process.env.NODE_ENV !== "production") {
1414
const name = this.displayName || this.constructor.name;
1515

16-
const isStaticReducer = typeof this.constructor.reducer === "function";
17-
const isPropertyReducer = typeof this.reducer === "function";
18-
19-
// @TODO: Remove property reducer support with v1.0.0
20-
if (isPropertyReducer) {
21-
console.warn(
22-
name +
23-
"(...): Class property `reducer` methods are deprecated. Please " +
24-
"upgrade to `static` reducers instead: " +
25-
"https://github.com/philipp-spiess/react-recomponent#why-is-the-reducer-static"
26-
);
27-
}
28-
29-
if (!(isStaticReducer || isPropertyReducer)) {
16+
if (typeof this.constructor.reducer !== "function") {
3017
throw new Error(
3118
name +
32-
"(...): No `reducer` method found on the returned component " +
33-
"instance: did you define a reducer?"
19+
"(...): No static `reducer` method found on the returned " +
20+
"component instance: did you define a reducer?"
3421
);
3522
}
3623
}
@@ -96,12 +83,7 @@ export function Re(Component) {
9683
let sideEffects;
9784

9885
const updater = state => {
99-
let reduced;
100-
if (typeof this.reducer === "function") {
101-
reduced = this.reducer(action, state);
102-
} else {
103-
reduced = this.constructor.reducer(action, state);
104-
}
86+
const reduced = this.constructor.reducer(action, state);
10587

10688
if (process.env.NODE_ENV !== "production") {
10789
if (typeof reduced === "undefined") {

type-definitions/ReComponent.js.flow

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ declare export function UpdateWithSideEffects<S, T>(
2121
declare export class ReComponent<
2222
Props,
2323
State,
24-
Action: { +type: string },
24+
Action: { +type: string }
2525
> extends React.Component<Props, State> {
2626
initialState(props: Props): State;
2727

@@ -39,11 +39,15 @@ declare export class ReComponent<
3939
|};
4040

4141
send(action: Action): void;
42-
createSender<A: Action>(actionType: $ElementType<A, 'type'>): (mixed) => A;
42+
createSender<A: Action>(actionType: $ElementType<A, "type">): mixed => A;
4343

4444
// This type is only used when initialState returns an Immutable.js data type.
4545
// Consider this API unstable.
4646
+immutableState: State;
4747
}
4848

49-
declare export class RePureComponent<P, S, A: { +type: string }> extends ReComponent<P, S, A> {}
49+
declare export class RePureComponent<
50+
P,
51+
S,
52+
A: { +type: string }
53+
> extends ReComponent<P, S, A> {}

0 commit comments

Comments
 (0)