Mastering card sliding and spacing in React NativeLearn how to effortlessly slide cards horizontally in your React

My aim with the following code snippet is to achieve two objectives:

  • Apply a margin of 20 units to each card
  • Display all four cards in a single row, allowing users to swipe horizontally

Unfortunately, I have not been successful in achieving either of these goals, despite my best efforts.

Feel free to experiment with the code on this playground: bit.ly/2V3KbKo

Below is the code snippet (which can also be copied and pasted into the link above):

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

export default class App extends React.Component {
  render() {
    return (
      <ScrollView style={stylesApp.cardContainer}>
        <MyCard index="514" black text="These are the news of Monday." style={stylesApp.myCard} />
        <MyCard index="514" black text="These are the news of Tuesday." style={stylesApp.myCard} />
        <MyCard index="514" black text="These are the news of Wednesday." style={stylesApp.myCard} />
        <MyCard index="514" black text="These are the news of Thursday." style={stylesApp.myCard} />
      </ScrollView>
    );
  }
}

const stylesApp = StyleSheet.create({
  cardContainer: {
    flexDirection: 'row',
    flexWrap: 'nowrap',
    overflow: 'scroll',
  },
  myCard: {
    margin: 20,
  },
});

//------

class MyCard extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            text: props.text,
        };
    }
    render() {
        return (
            <View style={stylesMyCard.container}>
                <Text style={stylesMyCard.text}>{this.state.text}</Text>
            </View>
        );
    }
}

const stylesMyCard = StyleSheet.create({
    container: {
        width: 80,
        minHeight: 40,
        padding: 10,
        backgroundColor: '#000',
        alignItems: 'center',
    },
    text: {
        color: '#fff',
        fontSize: 10,
    }
});

If you have any suggestions on how to achieve the desired functionalities, please share your working source code.

Thank you!

Answer №1

Experiment with the horizontal and showsHorizontalScrollIndicator properties for ScrollView to eliminate the need for stylesApp.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Sharing a boolean state between two pages using Material UI components

One challenge I've been facing is passing a boolean useState value between two pages. I found a helpful solution on this Stack Overflow thread: How to call useState from another Page? The main goal here is to display a success alert on a separate pag ...

Vue verifies the readiness of two computed properties

Each time I want to determine when multiple computed values are ready, I create another computed property like isFizzAndBuzzReady. computed: { fizz () { return ['f', 'i', 'z', 'z'] // asynchronous data retriev ...

Tips for establishing communication between a server-side and client-side component in Next.js

I am facing a challenge in creating an interactive component for language switching on my website and storing the selected language in cookies. The issue arises from the fact that Next.js does not support reactive hooks for server-side components, which ar ...

Troubleshooting a CORS problem with connecting an Angular application to a Node server that is accessing the Spotify

I am currently working on setting up an authentication flow using the Spotify API. In this setup, my Angular application is making calls to my Node server which is running on localhost:3000. export class SpotifyService { private apiRoot = 'http://lo ...

PHP Header Redirect Not Redirecting Correctly

As a newcomer to PHP, I conducted some research and attempted to implement a solution found on Stack Overflow, but unfortunately, it did not work for me. My goal is to redirect users to another page after a specific code has been executed. Despite removing ...

Exploring the power of colors in Tailwind CSS and Material UI for your CSS/SCSS styling

Discovering unique color palettes like the Tailwind Color for Tailwind CSS and theMaterial UI colors has been inspiring. These collections offer a range of beautifully named colors from 100 to 900 and A100 to A400, reminiscent of font styles. I am intrig ...

Challenge with the submission event listener

I am struggling to create my first event listener for the submit button that should respond to both a click and an enter key press. Currently, it is not working for either event, and I am unsure why. I do not intend to send any data to another page; I simp ...

Using the .json method in Angular 7 for handling responses

While attempting to implement the function getProblems to retrieve all problems within its array, I encountered an error message appearing on res.json() stating: Promise is not assignable to parameters of type Problem[]. It seems that the function is ...

Take the attention away from the canvas

Is there a way to shift focus away from the canvas and onto input fields in my HTML code? Right now, the canvas is holding onto focus even when I'm clicking on text inputs. This prevents me from typing anything into them. Ideally, I'd like for t ...

Insert text into the cursor location within Summernote using JQuery

Currently, I am working on a flutter application where I have implemented the summernote editor using JQuery. ClipboardData data = await Clipboard.getData(Clipboard.kTextPlain); String txtIsi = data.text .replaceAll("'", '\&bsol ...

JSPM encountered an issue with installation from GitHub (404 error), but it is able to successfully install packages from npm

I am encountering a frustrating issue with my Package.json file for a GitHub repository in my organization. Attempting to pull it in via jspm is causing errors. { "name": "tf-modernizr", "version": "1.0.0", "description": "", "main": "modernizr.js ...

Cloudfront is consistently experiencing cache-misses when serving images through the HTML img tag, yet is achieving cache-hits with requests made through Postman or

I'm experiencing an unusual issue with my Cloudfront distribution and S3 origin bucket set up through Cloudformation/SAM. The S3 bucket specifically stores images. When I try to request an image using the src attribute, the network tab in dev-tools i ...

After a refresh, jQuery's mousenter event is not functioning

I am currently working on a fun project called Etch-a-Sketch for The Odin Project, and I have a jquery grid that can be redrawn with the click of a button. Initially, the grid allows the user to choose the size and then draw on it using .mouseenter. Howev ...

The system is unable to access the 'string' property of an undefined variable

I encountered an error in my browser window stating "Cannot read property 'string' of undefined" when attempting to import a module (import { Button } from 'react-lightning-design-system'), even though I am not utilizing this module. ...

The code for implementing the "Read More" feature is not functioning as intended

I have been experiencing an issue with the implementation of the "read more" feature on my website. Although the code seems to be functioning properly, it only works after pressing the read more button twice. This particular code is designed to detect the ...

Where could one possibly find the destination of the mysterious <circle>?

I want to implement a simple pulsating animation on a circle svg element. I attempted using a transform: scale(..,..) animation, but it seems to be shifting the entire circle within its container instead of just scaling the element itself. This is the cur ...

Angular 12 app does not have Express server searching for static files

Context I am in the process of transferring an Angular application to a GKE cluster. Unfortunately, due to company policy, the base docker image I am required to use does not support the installation of new software such as shell or Angular CLI commands l ...

Exploring the mechanics of callbacks in jQuery's Ajax functionality

Greetings fellow developers! I've embarked on a new programming project with the firm intention of writing cleaner and more easily maintainable code than ever before. However, I have encountered my old nemesis - jQuery AJAX returns. The last time I t ...

Deploy the Next.js app on Vercel without regenerating pages, and instead trigger the deployment only upon request

Greetings to my fellow developers, For the past year, I have been using Next.js on Vercel and so far, I am absolutely loving it. The ability to create SEO friendly React websites has been a game changer for me. However, as my website continues to grow and ...

Tips for preventing MUI accordion from expanding:

Using two components from MUI, the accordion and Modal. I have placed the Modal on the accordion summary. However, every time I click inside the modal, the accordion also receives the interaction and expands. I tried passing the open state of the Modal to ...