jacobtipp / bloc-state

Modern FlutterBloc ecosystem for TypeScript/React
https://jacobtipp.github.io/bloc-state/
MIT License
13 stars 0 forks source link

fix: BlocBuilders not receiving the changing Bloc instances provided higher in the component tree. #120

Closed fakalit closed 4 months ago

fakalit commented 4 months ago

Description When a BlocProvider instance rerenders and changes the bloc instance it provides, a BlocBuilder under the component tree still continues to be subcribed to the initial bloc.

Steps To Reproduce

This is my attempt at isolating the issue to its basics.

export function Box({ abloc } : { abloc : ABloc }){
  console.log(`Box received ${abloc.id}`);
  return <BlocProvider bloc={ABloc} create={abloc} >
    <BlocBuilder bloc={ABloc} builder={state => { 
      console.log(`Box blocbuilder rerenders ${state.id}`);
      return <div>{state.property}</div>; }
    } />
  </BlocProvider>;
}

When a different bloc is given the Box Component, it can be seen that it rerenders from the console log, but the bloc builder still continues log the initial bloc's id.

(first render)
Box received fe3d46d0-855d-496d-aa94-d07b8122f837
Box blocbuilder rerenders fe3d46d0-855d-496d-aa94-d07b8122f837

(second render)
LogBox rerenders 3e09ab8e-b1ac-482a-8d72-48f2fc5f2aee
Box blocbuilder rerenders fe3d46d0-855d-496d-aa94-d07b8122f837

Expected Behavior I expected the BlocProviderd to switch to the new bloc instance provided by the BlocProvider.

I'm not sure what's happening now is the intended behaviour, but the above pattern was usable in the original package. I appreciate help and any pointers that can help with its resolution. Thanks like always.

fakalit commented 4 months ago

Seems like adding a "key" to the provider resolves the matter. Thanks.

jacobtipp commented 2 months ago

Sorry this is late, just as a side note. BlocProvider takes a dependencies array. You can pass a new bloc instance to the dependencies array which will provide a new bloc instance.

fakalit commented 2 months ago

That helps a lot. Thanks for the clarification and the great work you've done on the library.