Skip to content

made customHook resemble more like native Hook #15

New issue

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

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

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

fir0j
Copy link

@fir0j fir0j commented Jan 10, 2020

Before

const useDataApi = (initialUrl, initialData) => {
const [url, setUrl] = useState(initialUrl);

  const [state, dispatch] = useReducer(dataFetchReducer, {
    isLoading: false,
    isError: false,
    data: initialData,
  });
...
}
function App() {
  const [query, setQuery] = useState('redux');
  const [{ data, isLoading, isError }, doFetch] = useDataApi(
    'http://hn.algolia.com/api/v1/search?query=redux',
    { hits: [] },
  );
...
}

After

// passed initial url as the default parameter
const useDataApi = (initialData, initialUrl = 'http://hn.algolia.com/api/v1/search?query=redux') => {
	const [ url, setUrl ] = useState(initialUrl);
	const [ state, dispatch ] = useReducer(dataFetchReducer, {
               //  passing isLoading and isError property is redundant here
		data: initialData
	});
...
}
function App() {
	const [ query, setQuery ] = useState('redux');
	const [ result, setUrl ] = useDataApi({ hits: [] });
	const { data, isLoading, isError } = result;
...
}
  • I think passing single argument in useDataApi() resemble more like native hook inside App() component.
  • Also since we are initializing our state object by dispatching init action, passing isLoading and isError are redundant in my opinion.

please let me know what you think of it ?

  • if you like these small changes, add me to the contributor.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant