-
Notifications
You must be signed in to change notification settings - Fork 32
New hooks #66
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
Merged
Merged
New hooks #66
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
96f0212
Add useId
i-am-the-slime 93b6fca
Add useTransition
i-am-the-slime 421ad19
Add useDeferredValue
i-am-the-slime 53a11e4
Add useSyncExternalStore
i-am-the-slime 06a6db0
Add useInsertionEffect
i-am-the-slime 5cbfaee
Fix signatures
i-am-the-slime 20411fc
Drop vendored Discovery
i-am-the-slime c9fe43d
Add tests for new hooks
i-am-the-slime 410f83e
Fix docstring
i-am-the-slime 9551c3a
Make test less flaky
i-am-the-slime 2f0cf09
Merge branch 'main' into new-hooks
maddie927 736cba5
remove redundant import
maddie927 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
module Test.Spec.React18HooksSpec where | ||
|
||
import Prelude | ||
|
||
import Control.Monad.Rec.Class (forever) | ||
import Data.Array as Array | ||
import Data.Foldable (for_, traverse_) | ||
import Data.Maybe (fromMaybe) | ||
import Data.Monoid (guard, power) | ||
import Data.String as String | ||
import Data.Tuple.Nested ((/\)) | ||
import Effect.Aff (Milliseconds(..), apathize, delay, launchAff_) | ||
import Effect.Class (liftEffect) | ||
import Effect.Ref as Ref | ||
import Foreign.Object as Object | ||
import React.Basic (fragment) | ||
import React.Basic.DOM as R | ||
import React.Basic.DOM.Events (targetValue) | ||
import React.Basic.Events (handler, handler_) | ||
import React.Basic.Hooks (reactComponent) | ||
import React.Basic.Hooks as Hooks | ||
import React.TestingLibrary (cleanup, fireEventClick, renderComponent, typeText) | ||
import Test.Spec (Spec, after_, before, describe, it) | ||
import Test.Spec.Assertions (shouldNotEqual) | ||
import Test.Spec.Assertions.DOM (textContentShouldEqual) | ||
import Web.DOM.Element (getAttribute) | ||
import Web.HTML.HTMLElement as HTMLElement | ||
|
||
spec ∷ Spec Unit | ||
spec = | ||
after_ cleanup do | ||
before setup do | ||
describe "React 18 hooks" do | ||
it "useId works" \{ useId } -> do | ||
{ findByTestId } <- renderComponent useId {} | ||
elem <- findByTestId "use-id" | ||
idʔ <- getAttribute "id" (HTMLElement.toElement elem) # liftEffect | ||
let id = idʔ # fromMaybe "" | ||
id `shouldNotEqual` "" | ||
elem `textContentShouldEqual` id | ||
|
||
it "useTransition works" \{ useTransition } -> do | ||
{ findByText } <- renderComponent useTransition {} | ||
elem <- findByText "0" | ||
fireEventClick elem | ||
elem `textContentShouldEqual` "1" | ||
|
||
it "useDeferredValue hopefully works" \{ useDeferredValue } -> do | ||
{ findByTestId } <- renderComponent useDeferredValue {} | ||
spanElem <- findByTestId "span" | ||
spanElem `textContentShouldEqual` "0" | ||
findByTestId "input" >>= typeText (power "text" 100) | ||
spanElem `textContentShouldEqual` "400" | ||
|
||
it "useSyncExternalStore" \{ useSyncExternalStore } -> do | ||
{ findByTestId } <- renderComponent useSyncExternalStore {} | ||
spanElem <- findByTestId "span" | ||
spanElem `textContentShouldEqual` "0" | ||
delay (350.0 # Milliseconds) | ||
spanElem `textContentShouldEqual` "3" | ||
|
||
it "useInsertionEffect works" \{ useInsertionEffect } -> do | ||
{ findByText } <- renderComponent useInsertionEffect {} | ||
void $ findByText "insertion-done" | ||
|
||
where | ||
setup = liftEffect ado | ||
|
||
useId <- | ||
reactComponent "UseIDExample" \(_ :: {}) -> Hooks.do | ||
id <- Hooks.useId | ||
pure $ R.div | ||
{ id | ||
, _data: Object.singleton "testid" "use-id" | ||
, children: [ R.text id ] | ||
} | ||
|
||
useTransition <- | ||
reactComponent "UseTransitionExample" \(_ :: {}) -> Hooks.do | ||
isPending /\ startTransition <- Hooks.useTransition | ||
count /\ setCount <- Hooks.useState 0 | ||
let handleClick = startTransition do setCount (_ + 1) | ||
pure $ R.div | ||
{ children: | ||
[ guard isPending (R.text "Pending") | ||
, R.button | ||
{ onClick: handler_ handleClick | ||
, children: [ R.text (show count) ] | ||
} | ||
] | ||
} | ||
|
||
useDeferredValue <- | ||
reactComponent "UseDeferredValueExample" \(_ :: {}) -> Hooks.do | ||
text /\ setText <- Hooks.useState' "" | ||
textLength <- Hooks.useDeferredValue (String.length text) | ||
pure $ fragment | ||
[ R.input | ||
{ onChange: handler targetValue (traverse_ setText) | ||
, _data: Object.singleton "testid" "input" | ||
} | ||
, R.span | ||
{ _data: Object.singleton "testid" "span" | ||
, children: [ R.text (show textLength) ] | ||
} | ||
] | ||
|
||
useInsertionEffect <- | ||
reactComponent "UseInsertionEffectExample" \(_ :: {}) -> Hooks.do | ||
text /\ setText <- Hooks.useState' "waiting" | ||
Hooks.useInsertionEffect unit do | ||
setText "insertion-done" | ||
mempty | ||
pure $ R.span_ [ R.text text ] | ||
|
||
useSyncExternalStore <- do | ||
{ subscribe, getSnapshot, getServerSnapshot } <- do | ||
subscribersRef <- Ref.new [] | ||
intRef <- Ref.new 0 | ||
-- Update the intRef every 100ms. | ||
launchAff_ $ apathize $ forever do | ||
delay (100.0 # Milliseconds) | ||
intRef # Ref.modify_ (_ + 1) # liftEffect | ||
subscribers <- subscribersRef # Ref.read # liftEffect | ||
liftEffect $ for_ subscribers identity | ||
|
||
pure | ||
{ subscribe: \callback -> do | ||
subscribersRef # Ref.modify_ (Array.cons callback) | ||
pure $ | ||
subscribersRef # Ref.modify_ (Array.drop 1) | ||
, getSnapshot: Ref.read intRef | ||
, getServerSnapshot: Ref.read intRef | ||
} | ||
|
||
reactComponent "UseSyncExternalStoreExample" \(_ :: {}) -> Hooks.do | ||
number <- Hooks.useSyncExternalStore | ||
subscribe | ||
getSnapshot | ||
getServerSnapshot | ||
pure $ R.span { _data: Object.singleton "testid" "span", children: [ R.text (show number) ] } | ||
|
||
in { useId, useTransition, useDeferredValue, useInsertionEffect, useSyncExternalStore } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😮 I never knew you could use
?
in identifiers........There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, it's a habit, I can remove it. It's not
?
butʔ
which is from a glottal stopThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have that in a snippets file so I can do question mark tab to get it.