简体   繁体   中英

styled components :hover with react-native and react-native-web

I'm using styled-components in React-Native App. Let's say I have link component:

import styled from 'styled-components/native';

const Link = styled.Text`
  color: 'red';

  &:hover {
    color: 'blue';
  }
`

After that, I 'compile' my React-Native code with react-native-web .

All is great expect that hover is not working. (Text color remains red on hover.)

My guess is that https://github.com/styled-components/css-to-react-native is removing the hover definition.

Any idea how to fix this?

You can use onMouseEnter and onMouseLeave like in the refs section of styled-components. Advanced guide .

For native mobile development hover doesn't have a definition, that's because there is no cursor on the screen like there is on desktop devices. As React Native for web simulates how RN works, the :hover selector loose its sense

You can use rn-css that works like styled-components but with better support for css in React-Native.

Just replace import styled from 'styled-components/native'; with import styled from 'rn-css'; and your code will work.

Try use TouchableHighlight.

You can define the backgroundcolor when your button it's pressed or not!

Follow the guide onhttps://facebook.github.io/react-native/docs/touchablehighlight

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM