Skip to content

Add React props hook.#1523

Merged
dmjio merged 3 commits into
masterfrom
onPropsChanged
May 29, 2026
Merged

Add React props hook.#1523
dmjio merged 3 commits into
masterfrom
onPropsChanged

Conversation

@dmjio
Copy link
Copy Markdown
Owner

@dmjio dmjio commented May 28, 2026

props hook 🪝

This is useful for a descendant Component to react to changes in its parent Component. Currently, hooks just alter the child View, without invoking the update function. Like mount / unmount, the props phase allows one to "hook" into props as they alter over time and process an action in response to these changes.

This further aligns miso w/ React specification parity. The props phase is an action that executes post Component draw. It receives both old props and new props as arguments.

Sharing should be preserved between old and new props.

-- defaults to 'Nothing'

-- | Interface proposal
onPropsChanged :: Maybe (props -> props -> action)
               -- ^ old   ^ new

-- | Example 'Action'
data Action = PropsChanged ButtonProps ButtonProps

-- | Example usage
test :: Component parent ButtonProps model Action
test = myComponent { onPropsChanged = Just PropsChanged }
  • Extend 'Component' to receive props phase callback function
  • Add prevComponentProps
  • Extend ComponentState with _componentPropsPhase
  • Add prevComponentProps Lens.

The analog in React would be to track the previous props manually using a hook (useRef).

import { useEffect, useRef } from 'react';

function usePrevious(value) {
  const ref = useRef();
  useEffect(() => {
    ref.current = value;
  }, [value]);
  return ref.current;
}

// Usage in your component
function MyComponent({ userId }) {
  const prevUserId = usePrevious(userId);

  useEffect(() => {
    if (prevUserId !== userId) {
      console.log(`User ID changed from ${prevUserId} to ${userId}`);
      // Fetch new user data here
    }
  }, [userId, prevUserId]);
}

dmjio and others added 3 commits May 28, 2026 11:55
This is useful for a descendant 'Component' to react to changes in its
parent 'Component'. Currently, hooks just alter the child 'View',
without invoking the 'update' function. Like `mount` / `unmount`, the
`props` phase allows one to "hook" into `props` as they alter over time.

This further aligns miso w/ React specification parity. The `props`
phase is an action that executes post `Component` draw. It receives
both old props and new props as arguments.

Sharing should be preserved between old and new props.

```haskell
-- defaults to 'Nothing'

-- | Interface proposal
props :: Maybe (props -> props -> action)
               -- ^ old   ^ new

-- | Example 'Action'
data Action = PropsChanged ButtonProps ButtonProps

-- | Example usage
test :: Component parent ButtonProps model Action
test = myComponent { props = Just PropsChanged }
```

- [x] Extend 'Component' to receive `props` phase callback function
- [x] Add `prevComponentProps`
- [x] Extend `ComponentState` with `_componentPropsPhase`
- [x] Add `prevComponentProps` `Lens`.
@dmjio dmjio merged commit 89f2e43 into master May 29, 2026
2 checks passed
@dmjio dmjio deleted the onPropsChanged branch May 29, 2026 03:38
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.

1 participant