sAleksovski / react-native-android-widget

Build Android Widgets with React Native
https://sAleksovski.github.io/react-native-android-widget/
MIT License
546 stars 22 forks source link

how to get saved data from widget with react-native-async-storage/async-storage? #70

Closed kimgh06 closed 4 months ago

kimgh06 commented 4 months ago

Hello.

I enjoy your package, but there's a problem.

When I try to get async storage item using react-native-async-storage/async-storage from the widget handler, the widget can not be resized and updated.

I could show my codes you read.

import React from 'react';
import { HelloWidget } from './HelloWidget';
import useStore from './store';

const nameToWidget = {
  // Hello will be the **name** with which we will reference our widget.
  Hello: HelloWidget,
};

export async function widgetTaskHandler(props) {
  const widgetInfo = props.widgetInfo;
  const Widget = nameToWidget[widgetInfo.widgetName];
  const { getAsyncItem } = useStore(s => s);
  const text= await getAsyncItem('text');

  switch (props.widgetAction) {
    case 'WIDGET_ADDED':
      props.renderWidget(<Widget text={text} />);
      break;

    case 'WIDGET_UPDATE':
      props.renderWidget(<Widget text={text} />);
      break;

    case 'WIDGET_RESIZED':
      props.renderWidget(<Widget text={text} />);
      break;

    case 'WIDGET_DELETED':
      // Not needed for now
      break;

    case 'WIDGET_CLICK':
      // Not needed for now
      break;

    default:
      break;
  }
}

I think there's a solution that I didn't think of.

How to get saved data from widget?

I will wait for your answer.

Thanks.

kimgh06 commented 4 months ago

Oh, god. I should not did use a custom hook using zustand. I just tried with AsyncStorage hook, and It worked.

Sorry for wasting your time.