Notice: intent to deprecate and remove className prop from View and Text
The className prop is going to be deprecated from View and Text (and anything else it might currently be available on). This prop was only supported as an escape-hatch for Twitter Lite's migration from a View implemented atop of CSS Modules to the React Native View.
Rough plan at the moment is:
- Deprecate
classNamein 0.10. Use ofclassNamewill start printing warnings in the console. - Remove
classNamein 0.11.
If you're relying on this undocumented escape-hatch please let me know the details.
I've used className in the past to get a reference to the dom node when a browser only work around was necessary. I tried to get the dom node trough the ref but couldn't find it.
Also I've used it in the past to apply css classes that have browser specific psuedo selectors to hide scrollbars for example. Since I can't find anything about using css psuedo classes in react native which doesn't make sense but is needed in the example above.
Any recommendations?
className is useful escape-hatch for browser only styles. I don't know how to float an image in another way. Sure RNW can be strict, but the evolution is incremental.
Maybe some prefix for web-only inline styles can fix it.
Just an update, I've currently replaced all views that used the className prop as escape hatch with divs which just seems to work like a react component instead of a react native web component. So a .web.js file could contain mixed components like: <View>, <Text>, <div>, <h2> etc.
e.g.
render() {
return <div className={'not-an-escape-hatch-but-mixed-components'}><Text>Hello</Text></div>;
}
Mixing with classNames is not safe
I don't know how to float an image
Use the float style
I know that I'm very late to the discussion as I haven't for some reason noticed this before.
I have some projects that are using React Native Web and CSS modules. If the className gets removed, then is there any easy way to bring back the support, for example enable with an option or a possibility to create a small library that adds support for className?
I personally really like the idea that RNW would be compatible with the Web and Web React with allowing the usage of className like it has done so far.
https://reactjs.org/docs/faq-styling.html#how-do-i-add-css-classes-to-components
Note that this functionality is not a part of React, but provided by third-party libraries. React does not have an opinion about how styles are defined; if in doubt, a good starting point is to define your styles in a separate *.css file as usual and refer to them using className.
Going forward, you should never mix CSS modules with React Native components. In practice that means doing this…
import classes from './styles.css';
const A = () => <View className={classes.root} />
…will have no impact on the presentation. But you can continue to use CSS modules with React DOM components, and those React DOM components can be used in the same web app as React Native components.
Going forward, you should never mix CSS modules with React Native components.
My case is a bit different because I'm using CSS Modules + Sass or PostCSS in my React Native projects. I have also been planning on starting to migrate Web projects to use RNW, but if the support for CSS modules is removed, then I can not share any components between my RN projects and Web.
I know that this is not a problem for most of the users, but it breaks the RN<->Web compatibility for me and a few other people who are using CSS pre-processors in React Native and might possibly want to render the code in a browser.
Here's the link to my React Native CSS modules project if you want to read more: https://github.com/kristerkari/react-native-css-modules
If your project is compatible with RN shouldn't it be compatible with RNW without relying on the className prop being passed to the DOM?
If your project is compatible with RN shouldn't it be compatible with RNW without relying on the
classNameprop being passed to the DOM?
I have created a Babel plugin that is used only on the React Native side to transform className prop to style. That way you can use className for both RN and Web.
That way I can use CSS modules on both platforms together with RNW.
To give you a better idea, I have created simple example apps to show how you can render regular CSS or Sass on Android, iOS, and Web: https://github.com/kristerkari/react-native-css-modules#example-apps
CSS modules doesn't have the same safety or resolving characteristics as React Native styles.
CSS modules doesn't have the same safety or resolving characteristics as React Native styles.
I am fully aware of that, but it also does not mean that you can not create well functioning Web apps using CSS modules.
I'm ok with the className support not being enabled by default, but it would be nice if there was a way to opt-in with a setting to enable it, or something similar.
@kristerkari You can make a higher order component with setNativeProps in useEffect. You can also leverage nativeID.
There are a few additional things that I would like to be considered related to RNW's CSS modules support.
- Writing regular CSS with React Native's constraints means that the CSS stays a lot simpler and easier to maintain. Styling with CSS files works the same way as using an Javascript object with styles and the
styleproperty in React Native, but it's just presented differently to the user. I have created a stylelint config that can be used to validate any CSS to be React Native compatible even if you only use it on the Web. I know that it's not the same as usingStyleSheet, but better than using CSS modules without any restrictions. - Using CSS modules allows CSS pre/post-processors to be used in React Native, and also allows the default RN styling to be extended. I have for example added support for CSS Media Queries and CSS viewport units to React Native. You can use Javascript libraries to get those features to React Native, but then you also need to use the same libraries when you want to render the components for Web.
- Removing
classNamefrom RNW obviously means that no CSS pre/post-processor can be used with it, making it less flexible as you can not do things like scripting in Sass or use libraries like postcss-preset-env, etc.
Looks like this change also affects the use of styled components with RNW.
An example from my project:
const Container = styled(View)`
flex: 1;
justify-content: center;
align-items: center;
background-color: #fff;
`;
↓
Warning: Using the "className" prop on <View> is deprecated.
so @necolas any chance of re-opening this issue and adding a setting to RNW that would remove the warnings and enable className?
I would be just fine with the className being disabled by default and not the recommended way to do styling with RNW, but that you could opt-in to use it when needed.
It seems to me that removing className support completely will just break a lot of libraries that could otherwise be compatible with RNW.
@necolas would be great to have support for styled-components or a way to disable the warnings
Use styled-components/native. But you're better off using RN directly
Use styled-components/native. But you're better off using RN directly
Ah, for some reason I did not notice the /native in the import because it was rendering fine in React Native without the /native in the import.
btw. @necolas the styled-component implementation got me thinking about one idea. Do you think that it would be possible to create a plugin that would turn CSS from a file to style objects that run through the RNW Stylesheet.create and that way is rendered the same way as other styles? Kind of the same idea as with DSS and how it resolves the classes inside className etc.
That way the user would still have the option to use CSS files and there would be no need to expose className prop.
When I try to use styled-components/native it throws an error about react-native not being what it's expecting. I guess that this has to do with aliasing react-native with react-native-web?
./node_modules/styled-components/native/dist/styled-components.native.esm.js
Attempted import error: 'react-native' does not contain a default export (imported as 'reactNative').
Did anyone get this working properly?