Skip to content

Conversation

@AndrewMusgrave
Copy link
Member

WHY are these changes introduced?

Convert examples to hooks

WHAT is this pull request doing?

Converting TextField, Toast, and TopBar class-based examples to hooks

STILL TO DO

I'm making quite a few pr's converting examples to hooks so I anticipate lots of unreleased.md conflicts, so I'll add the entry before I merge.

- Converted `TextField`, `Toast`, and `TopBar` examples to functional components ([#2135](https://github.com/Shopify/polaris-react/pull/2135))

How to 🎩

Make sure the behavior of the example mirrors the style guide

🎩 checklist

Copy link
Contributor

@LauraAubin LauraAubin left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@BPScott BPScott left a comment

Choose a reason for hiding this comment

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

A bunch more cases where we can simplify toggles and avoid creating two functions over time

function ToastExample() {
const [active, setActive] = useState(false);

const toggleActive = useCallback(() => setActive(!active), [active]);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const toggleActive = useCallback(() => setActive(!active), [active]);
const toggleActive = useCallback(() => setActive((state) => !state), []);

Copy link
Contributor

Choose a reason for hiding this comment

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

For my understanding, how does this not modify all of the values in state if we only want to change active?

Copy link
Member Author

Choose a reason for hiding this comment

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

For some reason, I assumed set* didn't accept functions or callbacks. Guess I was wrong about the former 😄 I'll comb through my other examples and update them where it's needed.

For my understanding, how does this not modify all of the values in state if we only want to change active?

When using hooks your able to have a single state opposed to state objects in class-based components. If you do decide to use an object to hold state using useState, you won't be able to partially update state as you can in class base components.

const [booleanValue, setBooleanValue] = useState(false)
const toggleBooleanValue = useCallback(() => setActive(booleanValue => !booleanValue), []);

Copy link
Member

Choose a reason for hiding this comment

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

Crosslinking ya to the other PR where I mentioned this, which has some explanations and links to docs #2126 (comment)

@AndrewMusgrave AndrewMusgrave merged commit 372bdb0 into master Sep 16, 2019
@AndrewMusgrave AndrewMusgrave deleted the hookify-examples-10 branch September 16, 2019 18:04
@dleroux dleroux temporarily deployed to production September 20, 2019 17:44 Inactive
@dleroux dleroux temporarily deployed to production September 23, 2019 15:28 Inactive
@dleroux dleroux temporarily deployed to production September 23, 2019 15:38 Inactive
@dleroux dleroux temporarily deployed to production September 23, 2019 16:59 Inactive
@dleroux dleroux temporarily deployed to production September 23, 2019 17:05 Inactive
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.

4 participants