Skip to content

feat(hook): add use-set-init-data hook#42

Open
jonathanfilbert wants to merge 2 commits intolynx-community:mainfrom
jonathanfilbert:feat/use-set-init-data
Open

feat(hook): add use-set-init-data hook#42
jonathanfilbert wants to merge 2 commits intolynx-community:mainfrom
jonathanfilbert:feat/use-set-init-data

Conversation

@jonathanfilbert
Copy link
Collaborator

@jonathanfilbert jonathanfilbert commented Jan 9, 2026

Resolves #35

Description

This PR introduces a new hook, useSetInitData, which combines useInitData and useState. This hook allows developers to initialize a component's state with the native initData provided by the native-side to Lynx page initialization lifecycle and exposes it as a modifiable state.

Changes

  • New Feature: Added src/useSetInitData.ts
  • Tests: Added unit tests in tests/useSetInitData.test.ts.
  • Documentation: Added bilingual documentation (EN/ZH) in docs/.
  • README: Updated README.md and README.zh-CN.md to include the new hook.

Usage Example

Here is a working example of how to use the useSetInitData hook in a component:

import { useSetInitData } from 'reactlynx-use';
import { root, text, view } from '@lynx-js/react';

export function App() {
  const [data, setData] = useSetInitData();

  const handleUpdate = () => {
    setData({
      ...data,
      count: (Number(data.count) || 0) + 1,
    });
  };

  return (
    <view>
      <text>Initial Data Content:</text>
      <text>{data.count}</text>
      
      <view bindtap={handleUpdate} style={{ marginTop: '20px', padding: '10px'}}>
        <text>Update State</text>
      </view>
    </view>
  );
}

root.render(<App />);

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.

[Feature]: Implement useSetInitData

1 participant