Skip to content

Commit

Permalink
Initialized very simple global state with a very simple example of usage
Browse files Browse the repository at this point in the history
  • Loading branch information
tomdng committed Mar 19, 2020
1 parent 6082a2a commit f4a96e7
Show file tree
Hide file tree
Showing 5 changed files with 1,310 additions and 1,154 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-helmet": "^5.2.1",
"reactn": "^2.2.6",
"styled-components": "^5.0.1"
},
"devDependencies": {
Expand Down
11 changes: 10 additions & 1 deletion src/components/app/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from "react";
/* eslint react/jsx-one-expression-per-line: 0 */
import React, { useGlobal } from "reactn";
import styled, { AnyStyledComponent } from "styled-components";
import "./normalize.css"; // Normalize CSS styles across all browsers

Expand Down Expand Up @@ -41,6 +42,8 @@ const Column: AnyStyledComponent = styled.div`
`;

const TutorialApp: React.FC = (): JSX.Element => {
const [step, setStep] = useGlobal("step");

return (
<StyledTutorialPage>
<Navbar />
Expand All @@ -53,6 +56,12 @@ const TutorialApp: React.FC = (): JSX.Element => {
</Column>
<Column>
<h3 className="text-center">Output will go here</h3>
<button
type="button"
onClick={(): Promise<{ step: number }> => setStep(step + 1)}
>
{step}: Click to increase
</button>
</Column>
</MainWrapper>
</StyledTutorialPage>
Expand Down
9 changes: 9 additions & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import "reactn";

// This sets the shape of our global state since we want our global
// state to be typed.
declare module "reactn/default" {
export interface State {
step: number;
}
}
6 changes: 5 additions & 1 deletion src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import React from "react";
import React, { setGlobal } from "reactn";

import { TutorialApp } from "../components/app";

setGlobal({
step: 1
});

const IndexPage = (): JSX.Element => <TutorialApp />;

export default IndexPage;
Loading

0 comments on commit f4a96e7

Please sign in to comment.