-
Notifications
You must be signed in to change notification settings - Fork 974
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
Ability to override default isComponentOfType implementation #1164
Ability to override default isComponentOfType implementation #1164
Conversation
How do you use it? I see there is a variable defined on the global scope of the module but are you overriding through window? The usage should be defined as well. I don't like too much the function to depend on an external variable definition, maybe there is a way to achieve a similar behavior using something more explicit like |
Hey, the variable is defined not in global scope but in scope of the module import React from 'react'
import { render } from 'react-dom'
import { AppContainer } from 'react-hot-loader'
import { overrideComponentTypeChecker } from 'react-toolbox'
import store from 'redux/configureStore'
import Root from 'components/Root'
overrideComponentTypeChecker((classType, reactElement) => (
reactElement && (
reactElement.type === classType ||
reactElement.type.name === classType.displayName
)
))
const throwError = ({ error }) => { throw error }
render(
<AppContainer errorReporter={throwError}>
<Root store={store} />
</AppContainer>,
document.getElementById('root'),
)
if (module.hot) {
module.hot.accept('components/Root', () => {
System.import('components/Root').then(RootModule => {
const UpdatedRoot = RootModule.default
render(
<AppContainer errorReporter={throwError}>
<UpdatedRoot store={store} />
</AppContainer>,
document.getElementById('root'),
)
})
})
} Let me know what do you think. I didn't get an idea about |
No your right, I think it makes sense. Now that the example is documented in the same PR we can merge it. Looks good, thanks! :) |
Such documentation should be in README. |
I suggest this change: Usage with react-hot-loader v3Since react-hot-loader v3 babel patch leads to issues with some components (see #1152, #1155) to allow hot reloading in dev mode you need to define your own type checker in your main entry point (see #1164 for details): import "react-hot-loader/patch"
import React from 'react'
import { overrideComponentTypeChecker } from 'react-toolbox'
overrideComponentTypeChecker((classType, reactElement) => (
reactElement && (
reactElement.type === classType ||
reactElement.type.name === classType.displayName
)
)) |
Looks good! PR? |
I was thinking about this addition. And here are my thoughts.
|
It shouldn't create any issues. Do you want to PR this fix? Makes total sense |
It would be great to still have an ability to change |
@javivelasco which version do you want as a PR? |
Ok, my vision is as such: import global from 'global';
let customChecker;
const hotLoaderUsed = typeof global.__REACT_HOT_LOADER__ !== 'undefined';
export function overrideComponentTypeChecker (providedChecker) {
customChecker = providedChecker;
}
export function defaultChecker (classType, reactElement) {
return reactElement && reactElement.type === classType;
}
export function defaultCheckerWithDisplayNameCheck(classType, reactElement) {
return reactElement && (
reactElement.type === classType
|| reactElement.type.name === classType.displayName
);
}
export default function isComponentOfType (classType, reactElement) {
if (customChecker) {
return customChecker(classType, reactElement);
} else if (hotLoaderUsed) {
return defaultCheckerWithDisplayNameCheck(classType, reactElement);
} else {
return defaultChecker(classType, reactElement);
}
} please comment if you accept this (with an extra dep 'global') and if you accept code without semicolons . I will provide a PR afterwards. |
To me it looks ok and functional. But it needs to be adapted to the code style defined by eslint which basically it's airbnbs (with semicolons) |
ok, I'll do that |
Actually I'm not sure why shouldn't we just make the check in the same function. |
please decide ;) |
I'm not sure about this anyway. I was trying the solution in a project with react-hot-loader and it didn't work nicely. Maybe we can just keep the ability to override the function so anybody can write its own. |
Maybe point 2 from #1164 (comment) is what we need? E.g. |
If it helps people with hot loader it's ok on my side :) |
Fixes #1148
Let me know if any fixes required.