This repository has been archived by the owner on Apr 30, 2021. It is now read-only.
forked from andrew-w-ross/typings-react-redux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.tsx
75 lines (61 loc) · 1.64 KB
/
test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/// <reference path="./react-redux.d.ts" />
import {Provider, connect} from "./react-redux";
import {createStore, IActionGeneric, combineReducers, IDispatch, bindActionCreators} from "redux";
import * as React from "react";
interface IState {
greeting: string;
}
function changeGreeting(greeting:string):IActionGeneric<string>{
return {
type : "CHANGE_GREETING",
payload : greeting
};
}
function greetingReducer(greeting: string = "world", action: IActionGeneric<string>) {
switch (action.type) {
case "CHANGE_GREETING": return action.payload;
}
return greeting;
}
const rootReducer = combineReducers<IState>({
greeting : greetingReducer
});
const store = createStore(rootReducer)
interface IGreeterProps{
subject? : string;
onGreet? : (newGreet:string) => any
};
function mapStateToProps(state:IState):IGreeterProps{
return {
subject : state.greeting
};
}
function mapDispatchToProps(dispatch:IDispatch):IGreeterProps{
return {
onGreet : bindActionCreators(changeGreeting,dispatch)
};
}
@connect(mapStateToProps, mapDispatchToProps)
class Greeter extends React.Component<IGreeterProps, {}>{
render(){
return (
<div>
<h1>Hello {this.props.subject}</h1>
<input type="text" ref="input"/>
<button onClick={() => this.greet()}></button>
</div>
);
}
greet(){
let htmlInput = this.refs["input"] as HTMLInputElement;
this.props.onGreet(htmlInput.value);
htmlInput.value = "";
}
}
//Just trying out the old style and the decorator
const ConnectedGreeter = connect(mapStateToProps, mapDispatchToProps)(Greeter)
const App = () => (
<Provider store={store}>
<ConnectedGreeter></ConnectedGreeter>
</Provider>
);