Skip to content

Instantly share code, notes, and snippets.

View JaysonChiang's full-sized avatar

Jayson JaysonChiang

View GitHub Profile
const addCount = () => ({
type: "ADD_COUNT"
});
export default {
addCount
};
import { useActions } from "./useActions";
const AddButton = () => {
const { addCount } = useActions();
const onClick = () => {
addCount();
};
return <button onClick={onClick}>+1</button>;
import { useDispatch } from "react-redux";
import actionCreators from "./action-creators";
const AddButton = () => {
const dispatch = useDispatch();
const onClick = () => {
dispatch(actionCreators.addCount());
};
@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[];
@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[];
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 / 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;
}
@JaysonChiang
JaysonChiang / Search.jsx
Last active February 27, 2021 10:18
Simple React Refs Sample
import { useState, useRef, useEffect } from 'react';
const Search = () => {
const inputRef = useRef(null);
const [name, setName] = useState('');
useEffect(() => {
inputRef.current.focus();
}, []);
const EventComponent: React.FC = () => {
const onChange = (event: React.ChangeEvent<HTMLInputElement>) => {
console.log(event.target.value);
};
const onDragStart = (event: React.DragEvent<HTMLDivElement>) => {
console.log(event);
};
return (
const EventComponent = () => {
const onChange = (event) => {
console.log(event);
};
return (
<div>
<input onChange={onChange} />
</div>
);