Customizing the style of a component is always possible, let's say you have a component:
const MyComponent = ({size, color}) => <Text style={{color: color, fontSize:size}}>Hello</Text>;
In your parent component, you can fetch your theme data from the server side like this:
const themeJson = retrieveTheme() // fetching data from API
If you have color
and size
in your retrieved JSON data, you can pass them to MyComponent
like this:
<MyComponent color={themeJson.color} fontSize={themeJson.size} />
This will adjust the text size and color based on the theme JSON data.
In React Native, styling works differently compared to React. You need to use StyleSheet
to create styles instead of a simple JSON object. If you want to customize styles dynamically while rendering, you can combine styles in an array like so:
const styles= StyleSheet.create({
existStyle={
color: "red",
fontSize: 15
}
});
const MyComponent = ({size, color}) =>
<Text style={[styles.existStyle, {color: color, fontSize: size}] style={{color: color, fontSize:size}}>Hello</Text>;
This approach will override the predefined style at the top. Hopefully, this explanation helps clarify things for you!