Skip to content

Instantly share code, notes, and snippets.

View JaysonChiang's full-sized avatar

Jayson JaysonChiang

View GitHub Profile
@JaysonChiang
JaysonChiang / Search.tsx
Created February 27, 2021 10:31
React Refs with TypeScript
import { useState, useRef, useEffect } from 'react';
const Search: React.FC = () => {
const inputRef = useRef<HTMLInputElement | null>(null);
const [name, setName] = useState('');
useEffect(() => {
if (!inputRef.current) {
return;
}
const ADD_TODO = 'add_todo';
const ADD_TODO_SUCCESS = 'add_todo_success';
const ADD_TODO_ERROR = 'add_todo_error';
const initialState = {
loading: false,
error: null,
data: [],
}
@JaysonChiang
JaysonChiang / reducer.ts
Last active February 28, 2021 16:26
interface for state and action
enum ActionType {
ADD_TODO = 'add_todo',
ADD_TODO_SUCCESS = 'add_todo_success',
ADD_TODO_ERROR = 'add_todo_error',
}
interface TodoState {
loading: boolean;
error: string | null;
data: string[];
@JaysonChiang
JaysonChiang / reducer.ts
Last active February 28, 2021 16:26
Adding an Action Type Enum
enum ActionType {
ADD_TODO = 'add_todo',
ADD_TODO_SUCCESS = 'add_todo_success',
ADD_TODO_ERROR = 'add_todo_error',
}
interface TodoState {
loading: boolean;
error: string | null;
data: string[];
import { useDispatch } from "react-redux";
import actionCreators from "./action-creators";
const AddButton = () => {
const dispatch = useDispatch();
const onClick = () => {
dispatch(actionCreators.addCount());
};
import { useActions } from "./useActions";
const AddButton = () => {
const { addCount } = useActions();
const onClick = () => {
addCount();
};
return <button onClick={onClick}>+1</button>;
const addCount = () => ({
type: "ADD_COUNT"
});
export default {
addCount
};
@JaysonChiang
JaysonChiang / TodoList.tsx
Created March 2, 2021 00:54
useSelector without DefaultRootState
import { useSelector } from 'react-redux';
const TodoList: React.FC = () => {
const { todos } = useSelector((state) => state.todos);
// Property 'todos' does not exist on type 'DefaultRootState'.
return (
<ul>
{todos.map((todo) => (
<li>{todo}</li>
@JaysonChiang
JaysonChiang / TodoList.tsx
Created March 2, 2021 01:21
useSelector with DefaultRootState
import { useSelector } from './useSelector';
const TodoList: React.FC = () => {
const { todos } = useSelector((state) => state.todos);
return (
<ul>
{todos.map((todo) => (
<li>{todo}</li>
))}
const joe = {
update(message) {
console.log(`Joe receive ${message}`);
}
}
const jason = {
update(message) {
console.log(`Jayson read ${message}`);
}