Posts react typescript redux
Post
Cancel

react typescript redux

react typescript example

create react

1
2
npx create-react-app --template typescript react-typescript-redux
yarn add redux react-redux

1. without redux

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
// InputNoteInput.tsx 
import React, { ChangeEvent } from "react";

interface NewNoteInputProps {
    addNote(note:string): void
}
export const NewNoteInput: React.FC<NewNoteInputProps> = ({ addNote }) => {
    const [note, setNote] = React.useState("")
    const updateNote = (event:ChangeEvent<HTMLInputElement>) => {
        setNote(event.target.value)
    }

    const onAddNoteClick = () => {
        addNote(note)
        setNote("")
    }
    
  return (<div>
      <input onChange={updateNote} value={note} name="note" type="text" placeholder="note" />
      <button onClick={onAddNoteClick}>Add note</button>
  </div>)
};


// App.tsx
import React from 'react';
import { NewNoteInput } from './NewNoteInput';

function App() {
  return (
   <>
    <NewNoteInput addNote={alert}/>
   </> 
  );
}

export default App;

2. add redux

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// store.ts

import { createStore } from "redux";
import { noteReducer } from "./noteReducer";
export const store = createStore(noteReducer);


// index.ts
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import reportWebVitals from './reportWebVitals';
import {Provider} from 'react-redux'
import {store} from './store'
ReactDOM.render(
  <React.StrictMode>
    <Provider store={store}>
    <App />
    </Provider>
  </React.StrictMode>,
  document.getElementById('root')
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();



// noteReducer.ts
export interface NotesState {
  notes: string[];
}

const initialState = {
  notes: [],
};

type Action = { type: "ADD_NOTE"; payload: string };

export const noteReducer = (
  state: NotesState = initialState,
  action: Action
) => {
  switch (action.type) {
    case "ADD_NOTE": {
      return { ...state, notes: [...state.notes, action.payload] };
    }
    default:
      return state;
  }
};





// App.ts

import { useDispatch, useSelector } from "react-redux";
import { NewNoteInput } from "./NewNoteInput";
import { NotesState } from "./noteReducer";

function App() {
  const notes = useSelector<NotesState, NotesState["notes"]>(
    (state) => state.notes
  );
  const dispatch = useDispatch();
  const addNote = (note: string) => {
    console.log(note)
    dispatch({ type: "ADD_NOTE", payload: note });
  };
  return (
    <>
      <NewNoteInput addNote={addNote} />
      <hr />
      <ul>
        {notes.map((note) => (
          <li key={note}>{note}</li>
        ))}
      </ul>
    </>
  );
}

export default App;


run

3. action export

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
// actions.ts
export type Action = {type:"ADD_NOTE", payload:string}

export const addNote = (note:string):Action => ({
    type:"ADD_NOTE", payload:note
});

// noteReducer.ts
import { Action } from "./actions";

export interface NotesState {
  notes: string[];
}

const initialState = {
  notes: [],
};

export const noteReducer = (
  state: NotesState = initialState,
  action: Action
) => {
  switch (action.type) {
    case "ADD_NOTE": {
      return { ...state, notes: [...state.notes, action.payload] };
    }
    default:
      return state;
  }
};




// App.tsx
import { useDispatch, useSelector } from "react-redux";
import { NewNoteInput } from "./NewNoteInput";
import { NotesState } from "./noteReducer";
import {addNote} from "./actions"

function App() {
  const notes = useSelector<NotesState, NotesState['notes']>(
    (state) => state.notes
  );
  const dispatch = useDispatch();
  const onAddNote = (note: string) => {
    console.log(note)
    dispatch(addNote(note));
  };
  return (
    <>
      <NewNoteInput addNote={onAddNote} />
      <hr />
      <ul>
        {notes.map((note) => (
          <li key={note}>{note}</li>
        ))}
      </ul>
    </>
  );
}

export default App;

Link

https://www.youtube.com/watch?v=WpvIihorarA&list=PLIvCYh5AD3HxZ0J4bEEiSDJWeeN9mNNLL&index=3

This post is licensed under CC BY 4.0 by the author.