|
| 1 | +import React, {useState, createContext} from 'react'; |
| 2 | +import IncrementWithMoreHooks from './IncrementWithMoreHooks'; |
| 3 | + |
| 4 | +/** |
| 5 | + * This component as well as IncrementWithMoreHooks were made to show where data for different |
| 6 | + * hooks show up in the react fiber tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * This file is a JSX file, not a TSX file, on purpose. The code won't be converted to common JS |
| 11 | + * before being bundled by webpack. There were some errors that weren't showing up for the other |
| 12 | + * Increment.tsx file based on how webpack uglifies ES6 files. Maintaining this as a JSX file |
| 13 | + * will help check for these types of issues. |
| 14 | + */ |
| 15 | + |
| 16 | +/** |
| 17 | + * How Reactime extracts useState data and what would have to be done |
| 18 | + * to extract useContext and useReducer data: |
| 19 | + * |
| 20 | + * When extracting a functional component's useState data from the fiber tree in the backend of |
| 21 | + * Reactime, said data is stored on said component's fiber tree node at its memoizedState property, |
| 22 | + * which is a linked list with each node holding data for each useState invocation (some but |
| 23 | + * not all other hooks also store nodes with data here). Each useState memoizedState node includes |
| 24 | + * the variable (e.g. user) and its corresponding dispatch function (e.g. setUser). This dispatch |
| 25 | + * function is required to use Reactime's timeJump feature. |
| 26 | + * |
| 27 | + * useContext data is stored on the property "dependencies", and only the data passed into the |
| 28 | + * value attritibute of the 'context'.Provider element will be there. For tripContext.Provider, |
| 29 | + * we pass down "trip" without "setTrip", so we won't have access to the 'trip' dispatch function |
| 30 | + * in the "IncrementWithMoreHooks" component, meaning Reactime's timeJump won't work without |
| 31 | + * finding the 'trip' dispatch function by coming into this component where useState was invoked and |
| 32 | + * storing it in the appropriate place. This is easy enough for useState variables, but useContext |
| 33 | + * is commonly used with useReducer which is an entirely different beast. |
| 34 | + * |
| 35 | + * I advise solving the puzzle of making useReducer work with the timeJump functionality before |
| 36 | + * integrating other hooks. Look at time jumping in the Redux dev tools chrome extension for |
| 37 | + * inspiration, because you essentially need to recreate that within Reactime. |
| 38 | + */ |
| 39 | + |
| 40 | +// userInCreateContext is different from 'user' to see where this variable name showed up in the AST |
| 41 | +export const userContext = createContext({ userInCreateContext: 'null', setUser: undefined }); |
| 42 | +export const tripContext = createContext({ trip: 'null', setTrip: undefined }); |
| 43 | + |
| 44 | +const ButtonsWithMoreHooks = () => { |
| 45 | + const [user, setUser] = useState('null'); |
| 46 | + const userValue = { user, setUser }; |
| 47 | + const [trip, setTrip ] = useState('Hawaii'); |
| 48 | + const tripValue = { trip }; |
| 49 | + |
| 50 | + return ( |
| 51 | + <div className='buttons' key='increment container'> |
| 52 | + <userContext.Provider value={userValue} key="userContext Provider"> |
| 53 | + <tripContext.Provider value={tripValue} key="tripContext Provider"> |
| 54 | + <IncrementWithMoreHooks key={'IncrementWithMoreHooks'} /> |
| 55 | + </tripContext.Provider> |
| 56 | + </userContext.Provider> |
| 57 | + </div> |
| 58 | + ); |
| 59 | +} |
| 60 | + |
| 61 | +export default ButtonsWithMoreHooks; |
0 commit comments