有没有办法将pressed属性传递给样式化组件?
我现在所拥有的:
import React from 'react';
import { Pressable, Text, View } from 'react-native';
import styled from 'styled-components/native';
const StyledPressable = styled(Pressable)``;
const App = () => {
return (
<View>
<StyledPressable
onPress={() => null}
android_ripple={{ color: 'black', borderless: true }}>
<Text>Log in</Text>
</StyledPressable>
</View>
);
};
export default App;我想要实现的目标
import React from 'react';
import { Pressable, Text, View } from 'react-native';
import styled from 'styled-components/native';
const StyledPressable = styled(Pressable)`
background-color: ${props => pressed ? 'black' : 'blue'} // change color on press, eg.
`;
const App = () => {
return (
<View>
<StyledPressable
onPress={() => null}
android_ripple={{ color: 'black', borderless: true }}>
pressed={pressed} // this property "pressed" does not exist.
<Text>Log in</Text>
</StyledPressable>
</View>
);
};
export default App;This is the official docs。它使用内联样式,而我不能使用带样式的组件。
发布于 2020-10-19 03:25:34
我认为目前还没有办法。一种变通办法是在Pressable和Text之间使用View,并在其中执行所有样式:
import React from 'react';
import { Pressable, Text, View } from 'react-native';
import styled from 'styled-components/native';
const StyledView = styled.View`
background-color: ${({pressed}) => pressed ? 'black' : 'blue'}
`;
const App = () => {
return (
<View>
<Pressable onPress={() => null}>
{({pressed}) => (
<StyledView pressed={pressed}>
<Text>Log in</Text>
</StyledView>
)}
</Pressable>
</View>
);
};
export default App;https://stackoverflow.com/questions/64363375
复制相似问题