Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use hooks reducer instead #15

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/popup/src/components/Menu.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react'
import React, { useState, useReducer } from 'react'
import { Button } from 'antd'
import AddNewKeyword from './AddNewKeyword'

Expand Down Expand Up @@ -47,10 +47,22 @@ const useAddingKeyword = () => {

}

const initialState = {keyword: '', url: '', isAddKeyword: false};

function reducer(state: any, action:any) {
switch (action.type) {
case 'clearKeyword':
return initialState;
default:
throw new Error();
}
}

const Menu = () => {
const {keyword, setKeyword} = useKeyword()
const {url, handleSaveBookmark} = useGetUrl()
const {isAddKeyword, setIsAddKeyword} = useAddingKeyword()
const [state, dispatch] = useReducer<any>(reducer, initialState);

const handleAddKeyword = () => {
setIsAddKeyword(true)
Expand All @@ -60,8 +72,7 @@ const Menu = () => {

const handleStorage = () => {
window.localStorage.clear()
setIsAddKeyword(false)
setKeyword('')
dispatch({type: 'clearKeyword'})
}

if(keyword) {
Expand Down