-
Notifications
You must be signed in to change notification settings - Fork 558
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
RFC: useIsolation
#257
Open
Ayc0
wants to merge
20
commits into
reactjs:main
Choose a base branch
from
Ayc0:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
RFC: useIsolation
#257
Conversation
This file contains 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
If I understand correctly you want the equivalent of the following: Usage: import React from 'react'
export function App() {
console.log('render App')
return (
<IsolationProvider>
<Test />
</IsolationProvider>
);
}
function Test() {
console.log('render test')
const x = useIsolation(() => {
const [x, setX] = React.useState(0)
React.useEffect(
() => {
let interval = setInterval(() => setX(x => x + 1), 1000)
return () => { clearInterval(interval) }
},
[]
)
return React.useMemo(() => x - (x % 2), [x])
})
return <p>{x}</p>
} Non-optimized user space implementation: const isolationContext = React.createContext(null)
function useIsolation(unsafeHook) {
const hook = useEvent(unsafeHook)
const [result, setResult] = React.useState(null)
const registerHook = React.useContext(isolationContext)
React.useEffect(
() => registerHook({ hook, callback: (...args) => setTimeout(() => setResult(...args), 0) }),
[]
)
return result
}
function IsolationProvider({ children }) {
console.log('render isolation provider')
const [hookInfo, setHookInfo] = React.useState([])
const registerHook = React.useCallback(
(hookInfoToBeIsolated) => {
setHookInfo(existing => existing.concat(hookInfoToBeIsolated))
return function cleanup() {
setHookInfo(existing => existing.filter(info => info !== hookInfoToBeIsolated))
}
},
[]
)
return (
<isolationContext.Provider value={registerHook}>
{children}
{hookInfo.map((info, i) =>
// key should be handled differently
<Isolation key={i} {...{ info }} />
)}
</isolationContext.Provider>
)
}
function Isolation({ info }) {
const { callback, hook } = info
const result = hook()
console.log('hook executed', result)
useCallOnChange({ ifChanged: result, callback })
return null
}
function useCallOnChange({ ifChanged, callback }) {
const changeRef = React.useRef(null)
if (changeRef.current !== ifChanged) callback(ifChanged)
changeRef.current = ifChanged
}
function useEvent(f) {
const fRef = React.useRef(null)
fRef.current = f
return React.useCallback((...args) => fRef.current(...args), [])
} |
Yes, the idea is here with 2 differences:
|
Thank you for the RFC. I wanted to note that we’ve had a very similar proposal planned except that we wanted to roll this behavior into the existing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This RFC related to:
useIsolation
in isolation-hook and a new pattern to pass hooks to another(hook or hook creator) #168,useContextSelector
in RFC: Context selectors #119.This is about performance & memoization
>>> View rendered text <<<