What is the best way to import a reusable component from the theme folder in a React Native project?

I'm interested in importing a Button component that can be reused from the theme folder.

The path to the Button component is as follows:

\app\theme\components\Button.ts

Here is the code for Button.ts:

import { typography } from "theme/typography";
import { ButtonProps } from "react-native-elements";

export const Button = {
titleStyle: {
    fontSize: typography.size,
    fontFamily: typography.primaryMedium,
    lineHeight: typography.size,
    letterSpacing: 0,
    marginHorizontal: typography.size
},
containerStyle: {
    borderRadius: typography.size * 2 + typography.size * 2
},
buttonStyle: {
    borderRadius: typography.size * 2 + typography.size
}
} as ButtonProps

The specific component I want to import this Button into is named organizationdetails-screen.tsx.

Its location is at

\app\screens\superadmin-screens\organizationdetails-screen.tsx
.

My attempt at importing the Button looks like this:

import { Button } from '../../theme/components/Button';

However, I am encountering an error during this process.

You can view the error message here.

This is how I am utilizing the Button:

<Button
     title="Add User"
     onPress={() => this.props.navigation.navigate('AddNewUser')}
/>

Upon hovering over the button in Visual Studio Code, I receive the following error:

You can view the error details here.

Are there any suggestions on how to successfully import and use this reusable Button?

Answer №1

It seems that the button properties have been defined only in your \app\theme\components\Button.ts file and you are attempting to use them as a component, which will not function correctly.

To resolve this issue, modify your '\app\theme\components\Button.tsx' file with the following code:

import { StyleSheet } from 'react-native';
import { Button as RNButton, ButtonProps } from 'react-native-elements`;
import { typography } from "theme/typography";

const styles = StyleSheet.create({
  titleStyle: {
    fontSize: typography.size,
    fontFamily: typography.primaryMedium,
    lineHeight: typography.size,
    letterSpacing: 0,
    marginHorizontal: typography.size
  },
  containerStyle: {
    borderRadius: typography.size * 2 + typography.size * 2
  },
  buttonStyle: {
    borderRadius: typography.size * 2 + typography.size
  }
});

export const Button = (props: ButtonProps) => (
 <RNButton
  titleStyle={styles.titleStyle}
  containerStyle={styles.containerStyle},
  buttonStyle={styles.buttonStyle}
  {...props}
);

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

Can you identify the type of component that is returned from the withStyles() function?

My project includes a simple Dictionary component with basic properties: interface DictionaryProps { word: string; } In another component's props, I am looking for a generic component that only requires a string word: dictionary: React.ComponentC ...

The `role` property is not recognized in the type `User | AdapterUser` within NextAuth

In my attempt to develop a NextJS application integrated with NextAuth, I am facing an error in my [...nextauth].ts file while setting up the callbacks: Type error: Property 'role' does not exist on type 'User | AdapterUser'. Property ...

What is the best way to include 'SCSS' in the 'rollup' project that I have developed?

I am embarking on the creation of a React UI library and have chosen 'rollup' as my build tool. To enhance the project, I plan to incorporate TypeScript and utilize SCSS for styling. How can I implement SCSS within this setup? You can find the s ...

Select the implied type from a resolved Promise type in Typescript

I am working with a function called getStaticProps in Next.js that returns a Promise, resolving to an Object with the following structure: type StaticProps<P> = { props: P; revalidate?: number | boolean; } To generically "unwrap" the type o ...

Reacting like sticky bottoms and tops

I'm working on a react/tailwind project that involves a component I want to be fixed at both the top and bottom of the screen. In simpler terms, there's an element that I need to always stay visible even when the user scrolls up or down the page ...

Messaging Mishap with GCM

Here is a demo of GCM that works: If you refresh the page, you will notice a new random number. Only 25% of page refreshes actually send a message to GCM. Is this behavior normal or can I make some tweaks to the code to improve it? The page does not have ...

Creating a TypeScript declaration for the Cypress configuration file

When attempting to transition a setup-helper file to a ts definition, I encountered the following error message: Property 'domainName' does not exist on type 'Config' The error is related to this specific line of code: const { domainNa ...

Different method for uploading json documents to Firebase's live database

Recently, while following a Google codelabs tutorial on creating a chat app, I encountered an issue with importing a JSON file into the realtime database. The tutorial can be found at . The instructions were to access the Database section in the Firebase ...

Issues arising post transitioning to 14.0.0 from 13.0.0 version of ngx-masonry library leading to failed tests

Following the update to the latest stable version of the library ngx-masonry 14.0.0, our tests started failing. The release was just yesterday (24.10.2022) and you can find the changelog here: https://github.com/wynfred/ngx-masonry/blob/master/CHANGELOG.md ...

Accessing personal data on Android from the server

I have a couple of questions. First, I am currently developing an application that retrieves individuals' information from my server. What would be the most effective method for obtaining images from the server? Secondly, should I download the images ...

Error SCRIPT1002 was encountered in the vendor.js file while using Angular 8 on Internet Explorer 11

Having trouble getting Angular to function properly in IE 11. I've tried all the solutions I could find online. The errors I'm encountering are as follows: SCRIPT1002: Syntax error File: vendor.js, Line: 110874, Column: 40 At line 110874 args[ ...

Unit testing for Angular service involving a mock Http GET request is essential for ensuring the

I am seeking guidance on how to test my service function that involves http get and post calls. I attempted to configure the spec file by creating an instance of the service, and also consulted several sources on creating a mockhttp service. However, I enc ...

Sending a parameter between files in a React application: a step-by-step guide

I am currently working on a Pokedex website where I have Pokemon cards displaying data from a JSON file. When a user clicks on a card, a modal view appears with more detailed information about that specific card. I need help in ensuring that only the deta ...

Using type as an argument in a hook in a similar fashion to how it is

My custom hook utilizes Zustand and is capable of storing various data types. However, I am looking to specify the type of data that will be stored similar to how it is done with the useState hook. import { Profile } from "@/types"; import { crea ...

Tips for handling undefined values in observable next methods to return a default error message

I sent a request over the network and received a response. Whenever I encounter an undefined value in the response, I want to return a default error message. The response may contain multiple levels of nested objects. Is there a way to replace the if else ...

org.json.JSONException: The input ended unexpectedly at the beginning of the string in retrofit

I have recently developed an Android Application and am currently in the process of updating user profiles in the database. if (response.isSuccessful()) { Log.d("TAG", "response: "+response.body().string()); String result = response.bo ...

Modifying the text of a material UI button depending on a particular condition

I have a component that uses the Material UI button, and I need to change the text of the button based on a condition. If the order amount is 0, I want it to display "cancel", otherwise, it should show "refund". Can someone guide me on how to achieve thi ...

What is the best way to track events in angular-meteor when a user logs in, logs out, or when there is a change in the user

I am working on meteor-angular and trying to track new user login and logout changes within a single component. I have attempted to subscribe to userData in the component's initialization, but it does not seem to detect when the user logs in or out. I ...

Experience top-level security with our premium in-app purchases

Looking to implement Google Pay for a single non-consumable upgrade in my app, I've used the following code to check the status of the purchase: override fun onPurchasesUpdated( billingResult: BillingResult?, purchases: MutableList<Purchas ...

Troubleshooting: The issue of importing Angular 2 service in @NgModule

In my Angular 2 application, I have created an ExchangeService class that is decorated with @Injectable. This service is included in the main module of my application: @NgModule({ imports: [ BrowserModule, HttpModule, FormsModu ...