Skip to content
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

Making defaultProps an asynchronous function that returns an object? #496

Closed
Roshan54321 opened this issue Jun 5, 2024 · 9 comments
Closed

Comments

@Roshan54321
Copy link

I don't exactly know how I should populate the fields at first render (from external api). I don't want to call an api everytime the appState changes, rather call the api in defaultProps.

Is there a better way that exists?

@chrisvxd
Copy link
Member

chrisvxd commented Jun 5, 2024

You could probably do something with resolveData and check if any data exists before deciding whether or not to call your API, but perhaps not ideal.

Wonder if a resolveDefaultProps API could be a good idea to match resolveData and resolveFields 🤔

@Roshan54321
Copy link
Author

You could probably do something with resolveData and check if any data exists before deciding whether or not to call your API, but perhaps not ideal.

Wonder if a resolveDefaultProps API could be a good idea to match resolveData and resolveFields 🤔

yes, but I guess an async function that returns the desired object is enough for this.

@Roshan54321
Copy link
Author

@chrisvxd I was wondering if this is in your priority list. Would be great to have in coming release.

@chrisvxd
Copy link
Member

It's not on my radar right now, but should be easy to implement with the existing resolveData functionality.

This was referenced Jul 20, 2024
@chrisvxd
Copy link
Member

Closing in favour of #557

@chrisvxd chrisvxd closed this as not planned Won't fix, can't repro, duplicate, stale Aug 13, 2024
@Roshan54321
Copy link
Author

Roshan54321 commented Aug 13, 2024

Hi @chrisvxd. Sorry to mention again.

Regarding #557, i was thinking about the situation when you would want to give initial data to the component but using async requests. What i understand about resolveData is that it triggers everytime the data changes. I mean, it would be like i want to get 10 items for array type initially and let the user delete, add or edit according to his/her needs, but definitely don't want it to fetch 10 items again and again.

My concept is to get the initial data from sources and let the user do anything he wants after inserting and dont go with the overhead of resolveData.

I'm not very good with these things, but please correct me.

Ps: Thanks for looking at this.

@chrisvxd
Copy link
Member

@Roshan54321 I believe you could configure the resolveData API to only trigger once:

const config = {
  components: {
    HeadingBlock: {
      fields: {
        title: {
          type: "text",
        },
      },
      resolveData: async ({ props }) => {
        if (props.loaded) return;
       
        return {
          props: {
            title: "Hello, world",
            loaded: true
          },
        };
      },
      render: ({ title }) => {
        return <h1>{title}</h1>;
      },
    },
  },
};

@Roshan54321
Copy link
Author

Roshan54321 commented Aug 14, 2024

@Roshan54321 I believe you could configure the resolveData API to only trigger once:

const config = {
  components: {
    HeadingBlock: {
      fields: {
        title: {
          type: "text",
        },
      },
      resolveData: async ({ props }) => {
        if (props.loaded) return;
       
        return {
          props: {
            title: "Hello, world",
            loaded: true
          },
        };
      },
      render: ({ title }) => {
        return <h1>{title}</h1>;
      },
    },
  },
};

Yes, tried doing like that except, it didn't work on first load. This now looks fixed. But still i feel bad using resolveData (which definitely was made for another purpose) to mix my initialFetching logic.

Thanks for looking into it!

@chrisvxd
Copy link
Member

@Roshan54321 that's a fair concern. I generally prefer explicit APIs, too, but in this case worry about the amount of complex logic that underpins resolveData.

For the time-being, I don't think there's anything hugely wrong with using resolveData in this way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants