-
Notifications
You must be signed in to change notification settings - Fork 47.7k
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
React 16: className with bem-cn like libraries #10756
Comments
@ekfn can you expand on why you want to use a function with a custom |
@aweary Using function in className is very common if you want to build class name dynamically, for example in import block from 'bem-cn'
const b = block('MyBlock');
const render = () => (
<div className={b}>
<div className={b('title')}>My title</div>
<div className={b('content')}>My content</div>
</div>
);
// className={b} would return 'MyBlock'
// className={b('title')} would return 'MyBlock__title'
// etc So you can use |
This makes sense. I have no idea what the edge cases are if we were to allow functions with custom toString methods. Maybe this could be done with className only. I'm not really sure what the best call here is, but it would be great to identify if we want to continue to support this use case. |
Yeah, for className attribute I think it should works this way: convert value to string or die trying. For custom attributes though - not sure. I'm curious what so special about objects, that we try to convert it to string, but boolean values would be just ignored. And how react understands which attributes is |
bem-cn wrapper to return strings instead functions https://github.com/mistakster/bem-cn-lite |
@hrkv |
Why not just write |
this tool https://github.com/alfa-laboratory/cn-decorator |
@gaearon Actually, with import block from 'bem-cn'
const b = block('MyBlock');
const render = () => (
<div className={b()}>
<div className={b('title')()}>My title</div>
</div>
); @seriouslyfluffy doesn't it use function as well? |
But my question, in general, is why object with |
So, what we have:
|
Even if we called the function, we would still want to print a warning. Because most often passing a function happens due to a mistake, not deliberate (e.g. if you forget to call it). Silently ignoring it is worse because it usually means a bug. Since we warn, libraries shouldn't rely on it anyway, as we encourage people to treat warnings as errors. So that's why passing it through doesn't look like a solution to me. I don't quite understand the advantages of the chainable interface you're suggesting over simpler interface that accepts a variable number of arguments and always returns a string. I think that this is always possible. While React 16 behavior might force an API change in that library, I don't see any use cases that would work before but be impossible to implement now. So I think we can close this as for the vast majority of use cases the new behavior is better (it catches more bugs).
We released an RC of React 16 with this behavior a few weeks ago, and publicly asked people to test it. Now that 16 is out, it is too late to change the behavior anyway, and this library is the first use case relying on custom |
Yeah, in this case, this is pretty same as ignoring (silently or not).
Do i really need to explain why different interfaces are good and fun in deffirernt cases?
Thanks for suggestion, that's a good practice, but I don't use Personally, in this case, I still would prefer not changing API over |
Hi, i make codemod for the issue https://github.com/iseeyou911/jscodemodes/tree/master . May be it would help for someone. |
Hello! Really love new features and architecture of Fiber.
But because of this new behavior:
I've got this issue:
It wouldn't add className, even if i've override
toString
method, but with object it works:I think it's OK to have same behavior with function and object.
This feature is needed in "bem-cn" like library, so I can write just
fn
, instead offn()
orfn.toString()
The text was updated successfully, but these errors were encountered: