Skip to content

made customHook resemble more like native Hook#15

Open
erfirojsiddiki wants to merge 1 commit intothe-road-to-learn-react:masterfrom
erfirojsiddiki:master
Open

made customHook resemble more like native Hook#15
erfirojsiddiki wants to merge 1 commit intothe-road-to-learn-react:masterfrom
erfirojsiddiki:master

Conversation

@erfirojsiddiki
Copy link
Copy Markdown

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant