Add MobX for state management
This commit is contained in:
+21
-11
@@ -1,18 +1,28 @@
|
||||
import * as React from "react";
|
||||
import { observer } from "mobx-react";
|
||||
import "./App.scss";
|
||||
|
||||
const reactLogo = require("../../assets/img/react_logo.svg");
|
||||
import appState from "../../stores/AppStore";
|
||||
import Button from "../Button";
|
||||
|
||||
export interface AppProps {
|
||||
appState: {
|
||||
increment: () => string,
|
||||
counter: number
|
||||
};
|
||||
}
|
||||
|
||||
export default class App extends React.Component<AppProps, undefined> {
|
||||
render() {
|
||||
return (
|
||||
<div className="app__landing">
|
||||
<h1>Aalto-yliopiston sähköinsinöörikilta!</h1>
|
||||
<p>Sähköä, viinaa, naisia.</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@observer class App extends React.Component<AppProps, undefined> {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
}
|
||||
|
||||
render() {
|
||||
return <div className="app__landing">
|
||||
<h1>Aalto-yliopiston sähköinsinöörikilta!</h1>
|
||||
<p>Sähköä, viinaa, naisia.</p>
|
||||
<Button onClick={this.props.appState.increment}>{this.props.appState.counter}</Button>
|
||||
</div>;
|
||||
}
|
||||
}
|
||||
|
||||
export default props => <App appState={appState} {...props} />;
|
||||
@@ -0,0 +1,20 @@
|
||||
@import "../../index.scss";
|
||||
|
||||
.button {
|
||||
-webkit-appearance: none;
|
||||
border-radius: none;
|
||||
padding: 0.5rem 1rem;
|
||||
margin: 0.5rem;
|
||||
border: 2px solid $blue;
|
||||
font-size: 1rem;
|
||||
font-weight: 500;
|
||||
|
||||
&:hover {
|
||||
border: 2px solid $dark-blue;
|
||||
}
|
||||
|
||||
&:active,
|
||||
&:focus {
|
||||
outline: none;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import * as React from "react";
|
||||
import "./Button.scss";
|
||||
|
||||
export interface ButtonProps {
|
||||
onClick: () => void;
|
||||
}
|
||||
|
||||
export default class Button extends React.Component<ButtonProps, undefined> {
|
||||
render() {
|
||||
return (
|
||||
<button onClick={this.props.onClick} className="button">
|
||||
{this.props.children}
|
||||
</button>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
import Button from "./Button";
|
||||
export default Button;
|
||||
@@ -0,0 +1,13 @@
|
||||
import { observable, action } from "mobx";
|
||||
import { observer } from "mobx-react";
|
||||
|
||||
class AppStore {
|
||||
@observable counter = 0;
|
||||
|
||||
@action.bound
|
||||
increment() {
|
||||
this.counter += 1;
|
||||
}
|
||||
}
|
||||
|
||||
export default new AppStore();
|
||||
Reference in New Issue
Block a user