我正在學習一個教程,我目前有這個代碼,我試圖根據道具更改背景的顏色......這就是它的樣子
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
const SomeComponent = ({ bgColor }) => (
<View style={styles.wrapper(bgColor)}>
<Text style={styles.text}>3333</Text>
</View>
);
const styles = StyleSheet.create({
wrapper: color => ({
flex: 1,
backgroundColor: color,
}),
text: {
color: 'red',
},
});
我不斷收到一個styles.wrapper is not a function
請我如何解決這個問題...
uj5u.com熱心網友回復:
您不能像以前那樣宣告 StyleSheet 函式。
或者,您可以宣告一個JS函式,該函式根據您傳遞的引數傳遞不同顏色的相同樣式。
你可以這樣做:
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
const myStyle = (color) => {
return {
flex: 1,
backgroundColor: color,
}
};
const SomeComponent = ({ bgColor }) => {
<View style={this.myStyle(bgColor)}>
<Text style={styles.text}>3333</Text>
</View>
};
const styles = StyleSheet.create({
text: {
color: 'red',
},
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/399766.html
標籤:反应原生
