Skip to content

Conversation

@nataliaAnashkevich
Copy link
Owner

No description provided.


const searchErrorHandler = error => {
setError(error);
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to wrap the handler in useCallback hook. searchErrorHandler is being recreated for each call of FindLocation component and then searchErrorHandler is used as a dependency in useEffect. It means that you can get unexpected call of useEffect

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, will do

}, [searchFieldValue, onError]);

const handleChange = e => {
e.preventDefault();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you really need preventDefault here ?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it should be removed, thanks

};

const itemClickedHandler = (e, item) => {
onItemClick && onItemClick(item);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to use classical if statement here
if (onItemClick) { onItemClick(item); }

});
} else {
setSelectedLocation(undefined);
setCurrentWeather(undefined);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use null instead of undefined. Also provide null values as default for useState hooks


useEffect(async () => {
if (locationId) {
await invokeAction(async () => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please read doc "How to use async functions in useEffect"
https://devtrium.com/posts/async-functions-useeffect

{currentWeather && <p>Temperature: {currentWeather.current.temperature}%</p>}
{currentWeather && <p>Probability of precipitation: {currentWeather.current.precipProb}%</p>}
{currentWeather && <p>{capitalize(currentWeather.current.symbolPhrase)}</p>}
{currentWeather && <SafeWeatherImage symbolCode={currentWeather.current.symbol} />}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can extract temperature, precipProb, symbolPhrase, symbol variables before the return statment like this
const { temperature, precipProb, symbolPhrase, symbol } = (currentWeather || { current: {} }).current
then

{temperature && <p>Temperature: {temperature}%</p>}
{precipProb && <p>Probability of precipitation: {precipProb}%</p>}
{symbolPhrase && <p>{capitalize(symbolPhrase)}</p>}
{symbol && <SafeWeatherImage symbolCode={symbol} />}

function SearchItem({ item, onItemClick }) {
const itemClickHandler = useCallback(
e => {
e.preventDefault();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you really need preventDefault here ?
Please read about the method https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault

}

function SearchItem({ item, onItemClick }) {
const itemClickHandler = useCallback(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain please why you use useCallback here ?

@nataliaAnashkevich nataliaAnashkevich force-pushed the Anashkevich_Natalia_FindLocation_Search branch from 2b7feca to 09e5f45 Compare August 29, 2022 22:35
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.

3 participants