Skip to content

Commit

Permalink
Refactor existing components for containers and react router
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Roger Lux committed Sep 26, 2018
1 parent 9848ac9 commit 0d1fa6c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 56 deletions.
63 changes: 10 additions & 53 deletions src/common/App.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,18 @@
import * as React from "react";
import { connect } from "react-redux";
import { Switch, Route } from "react-router-dom";

import { Store } from "common/redux/store"
import { changeTitle } from "common/redux/action"
import { Title } from "common/component/Title"
import { Button } from "common/component/Button"

interface AppProps {
title: string;
updateTitle: any;
}

class App extends React.Component<AppProps> {
titleList: string[] = [
"Hello World!",
"High five from React",
"Wow. Much skills."
];

constructor(props: AppProps) {
super(props);

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

public setRandomTitle() {
let titleIndex = this.titleList.indexOf(this.props.title) + 1;
if (titleIndex >= this.titleList.length) {
titleIndex = 0;
}

const newTitle = this.titleList[titleIndex];
this.props.updateTitle(newTitle);
}
import Home from "common/container/Home";
import Test from "common/container/Test";

export default class App extends React.Component {
public render() {
return (
<div className="App">
<Title>
{this.props.title}
</Title>
<Button onClick={this.setRandomTitle}>
Update
</Button>
</div>
<main>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/test" component={Test} />
</Switch>
</main>
);
}
}

const mapStateToProps = (state: Store) => {
return {
title: state.title
}
}

const mapDispatchToProps = {
updateTitle: changeTitle
}

export default connect(mapStateToProps, mapDispatchToProps)(App)
4 changes: 2 additions & 2 deletions src/common/component/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import * as React from "react";
import styled, { css } from "styled-components";

interface ButtonProps {
onClick: any;
children: string;
children?: any;
onClick?: any;
primary?: boolean;
}

Expand Down
2 changes: 1 addition & 1 deletion src/common/component/Title.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from "react";
import styled from "styled-components";

interface TitleProps {
children: string;
children?: any;
}

export const Title = (props: TitleProps) => {
Expand Down

0 comments on commit 0d1fa6c

Please sign in to comment.