-
Notifications
You must be signed in to change notification settings - Fork 208
Regression: GlamorousComponent being a class instead of stateless function causes issue with wrapped inputs #58
Comments
Thank you for filing this bug! What do you think we need to do to fix it? |
Hm... not too sure about how easy this is gonna be, but one approach would be to not return a new return function GlamorousComponent(props) {
return (
<GetTheme>{theme => doTheUsualStuff(props, theme)}</GetTheme>
)
} |
Or even return function GlamorousComponent(props) {
withTheme(({theme}) => doTheUsualStuff({theme, ...props}))
} Which also would get rid of all the duplicated code for theme subscriptions! |
|
oh, yes. You're absolutely right. So one work-around combining both approaches could be: const GetTheme = ({children}) => withTheme(({theme}) => children(theme)) and then using it as described above: return function GlamorousComponent(props) {
return (
<GetTheme>{theme => doTheUsualStuff(props, theme)}</GetTheme>
)
} or not even loading the theme from context if the return function GlamorousComponent(props) {
return props.theme ? (
doTheUsualStuff(props, props.theme)
) : (
<GetTheme>{theme => doTheUsualStuff(props, theme)}</GetTheme>
)
} Note that the last example would lead to remounting the underlying component when |
Would this change affect |
yes, it would affect |
Wow, is there any technical difference between class and stateless function components? |
Ok, after looking at this more closely, I don't think that this is something I want to complect the codebase to support directly. However! There is a relatively simple workaround that you could use while still preserving the API you're looking for. For example: import React from 'react';
import { render } from 'react-dom';
import glamorous from 'glamorous';
import memoize from 'fast-memoize';
const getComponent = memoize(Comp =>
glamorous(Comp)({
border: '1px blue solid',
}),
);
const wrapInput = (Comp, props) =>
React.createElement(getComponent(Comp), props);
class App extends React.Component {
state = { value: '' };
render() {
return (
<div>
<h2>Form</h2>
{wrapInput('input', {
value: this.state.value,
onChange: e => this.setState({ value: e.target.value }),
})}
</div>
);
}
}
render(<App />, document.body); I hope that's sufficient. I consider this to be kinda edge-casey. Feel free to disagree with me and I'd be happy to review a PR that doesn't make things much more complex. Good luck! And I'm glad you like glamorous :) |
Okay, thank's for showing a possible workaround! :) Some comments:
|
Understood. I expect a lot of the work for #42 to be pretty invasive. If we can do it in several smaller PRs that would make reviewing faster and easier :) I'd love to see some refactor PRs that don't actually necessitate a new release, but make it so making new changes is easier 👍 |
glamorous
version: 3.6.0Check this simplified example of my code: https://codesandbox.io/s/L8OYqz1Wv
Since the result of
glamorous(Comp)
is now a class rather than a function, react now remounts the component on each rerender causing inputs to break. (Just start typing in the example. This used to work before introducing the theming support).I am aware that the code-example can be rewritten quite easily to make it work, but the form framework I'm using encourages that pattern of the code example.
And finally: Thanks for this awesome library! This actually is the first time I'm feeling like I can stick with a styling solution for a long long time 😃
The text was updated successfully, but these errors were encountered: