Skip to content

feat: React Bloc: BlocProvider & BlocConsumer#29

Open
prateek14 wants to merge 5 commits intofelangel:masterfrom
prateek14:master
Open

feat: React Bloc: BlocProvider & BlocConsumer#29
prateek14 wants to merge 5 commits intofelangel:masterfrom
prateek14:master

Conversation

@prateek14
Copy link
Copy Markdown

@prateek14 prateek14 commented Sep 8, 2020

Status

IN DEVELOPMENT

Breaking Changes

NO

Description

APIs for BlocProvider and BlocConsumer using React Context API

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • 📝 Documentation
  • 🗑️ Chore

Copy link
Copy Markdown
Collaborator

@erickjtorres erickjtorres left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is really cool. looks like test phase is failing in the build because they have not been updated to use the new bloc api

Comment thread packages/react-bloc/lib/src/bloc-provider.tsx Outdated
@felangel felangel added the enhancement New feature or request label Sep 8, 2020
@prateek14
Copy link
Copy Markdown
Author

I'll update the test phase

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Sep 10, 2020

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

<BlocBuilderInternal bloc={props.bloc} builder={props.builder} condition={props.condition} />
)
} else if (props.type) {
const context = BlocProvider.context<B>(props.type)
Copy link
Copy Markdown
Collaborator

@erickjtorres erickjtorres Sep 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also noticed we are passing in the type manually. Can we get the name of the bloc using something like props.bloc.constructor.name ?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

props.bloc.constructor.name will be minified with any good builder, eg Webpack.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, my first iteration used constructor.name, but it doesnt work in following cases:

  1. Minification
  2. Old browsers, <IE9
  3. Classes defined using Prototypical inheritance es5

The new react library, Recoil also uses a unique key method to track a set of state nodes.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to note, there is one more possibility, see this implementation https://github.com/cartant/ts-action/blob/master/packages/ts-action/source/action.ts

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think we should consider trying to implement something that removes the need to pass in the type. I think someone also mentions using metadata in https://stackoverflow.com/questions/13613524/get-an-objects-class-name-at-runtime.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could merge the PR now and have this as a separate issue to look into. @felangel what do you think?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delayed response! Going to take a closer look at this tomorrow and let y'all know what my thoughts are 👍

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erickjtorres Even if we were to implement an instance property called "type" as suggested by Lonli or use something like constructor.name, they will be available only in the instance context.
We need to be able to access the key in a static context

const context = BlocProvider.context<B>(props.type) 
// No way to access instance properties of B without creating an object first

Comment thread packages/react-bloc/test/bloc-provider.test.tsx
@prateek14 prateek14 changed the title React Bloc: BlocProvider & BlocConsumer feat: React Bloc: BlocProvider & BlocConsumer Sep 24, 2020
@MrSquaare
Copy link
Copy Markdown

👋 What's the current status of this PR?

@felangel
Copy link
Copy Markdown
Owner

👋 What's the current status of this PR?

Hey thanks for bringing this back to my attention! I'll try to take another this this week and hopefully clean up and get something release very soon 👍

@Lonli-Lokli
Copy link
Copy Markdown

Hei @felangel did you have a chance to check it?

@Rukkaitto
Copy link
Copy Markdown

This is the only absent feature that is keeping me from using bloc.js in production. Would be great if this was merged.

@felangel
Copy link
Copy Markdown
Owner

I'll try to take a look over the weekend and get this merged, sorry for the delay here!

@lgoudin
Copy link
Copy Markdown

lgoudin commented Jan 19, 2022

news on this? would be a nice alternative to passing the bloc through useContext

@felangel
Copy link
Copy Markdown
Owner

news on this? would be a nice alternative to passing the bloc through useContext

Sorry keep getting side-tracked! Will try to make time today 👍

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

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants