Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native: Styling not applying

I cant understand why styling is not being applied to the {props.children} inside view in the following code. Its not throwing any errors but only renders the {props.children} as simply text.

import React from 'react';
import { View } from 'react-native';

const Card = (props) => {
  return (
    <View style={styles.containerStyle}>
      {props.children}
    </View>
  );
};

const styles = {
  containerStyle: {

    borderWidth: 8,
    borderRadius: 2,
    borderColor: 'black',
    borderBottomWidth: 0,
    shadowColor: 'black',
    shadowOffset: { width: 0, height: 2 },
    shadowOpacity: 0.1,
    shadowRadius: 2,
    elevation: 1,
    marginLeft: 5,
    marginRight: 5,
    marginTop: 60
  }
};

export default Card;

render {props.children} here:

import React from 'react';
import { Text } from 'react-native';
import Card from './Card';

const AlbumDetail = (props) => (
    <Card>
      <Text>{props.album.title}</Text>
    </Card>
  );

export default AlbumDetail;
like image 295
James Mulcahy Avatar asked Sep 13 '25 18:09

James Mulcahy


2 Answers

@Murmeltier's answer has tiny error.

Text component has the name of property, 'style', not 'styles'.

<Text style={props.album.title}></Text>
like image 147
Lanon Avatar answered Sep 17 '25 18:09

Lanon


You are just passing your nested components to your AlbumDetail but you are not applying any style to the text inside of your Card-component. You should set them like this:

import React from 'react';
import { Text } from 'react-native';
import Card from './Card';

const AlbumDetail = (props) => (
    <Card>
      <Text styles={props.album.title}></Text>
    </Card>
);

export default AlbumDetail;
like image 30
Murmeltier Avatar answered Sep 17 '25 19:09

Murmeltier