0

Good evening,

I have been trying to create custom card Component in react native that i can use everywhere i need it.

I apply a part of the style inside the component itself, and for the width, layout etc, where i need it. Then i am trying to use the spread operator to "merge" all the properties in the style of the child component. I saw it in a video tutorial, but it's not working for me, and i don't see what is wrong in my code :

import React from 'react';
import { View, Text, StyleSheet, TextInput, Button } from 'react-native';

import Card from '../components/Card';
import Color from '../constants/colors'
import Input from '../components/Input'

const StartGameScreen = props => {
  return (
    <View style={styles.screen}>
      <Text style={styles.title}>Start a New Game!</Text>
      <Card style={styles.inputContainer}>
        <Text>Select a Number</Text>
        <Input />
        <View style={styles.buttonContainer}>
          <Button title="Reset" color={Color.accent} onPress={() => {}} />
          <Button title="Confirm" color={Color.primary} onPress={() => {}} />
        </View>
      </Card>
    </View>
  );
};

const styles = StyleSheet.create({
  screen: {
    flex: 1,
    padding: 10,
    alignItems: 'center'
  },
  title: {
    fontSize: 20,
    marginVertical: 10
  },
  inputContainer: {
    width: 300,
    maxWidth: '80%',
    alignItems: 'center',
  },
  buttonContainer: {
    flexDirection: 'row',
    width: '100%',
    justifyContent: 'space-between',
    paddingHorizontal: 15
  },
  input: {
    width: 50
  }
});

export default StartGameScreen;

And the child component :

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

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

const styles = StyleSheet.create({
  card: {
    shadowColor: 'black',
    shadowOffset: { width: 0, height: 2 },
    shadowRadius: 6,
    shadowOpacity: 0.26,
    elevation: 8,
    backgroundColor: 'white',
    padding: 20,
    borderRadius: 10
  }
});

export default Card;

Note : I tried with input and card, it's working for none of them.

Is that something that we were able to do but we can't anymore ?

Thank you

Have a nice weekend

Mikev60
  • 103
  • 1
  • 8
  • Does this answer your question? [React Native Reusable Card Component won't Visualize as expected](https://stackoverflow.com/questions/61758279/react-native-reusable-card-component-wont-visualize-as-expected) – Muhammad Haekal Nov 22 '20 at 17:28

1 Answers1

0

Try this:

const Card = props => {
  return (
    <View style={[styles.card, props.style ]}> {props.children}</View>
  );
};


Ketan Ramteke
  • 10,183
  • 2
  • 21
  • 41
  • 1
    Thank you, it is working, but i still don't get why the line i typed following the tutorial is not working ... :) – Mikev60 Nov 22 '20 at 16:36