Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare model class in react native and pass whole model class in second screen by use of react navigation

Tags:

react-native

Is proper way to declare model class like java and how to pass this into second screen in react native?

export default class UserModel {

    stateName;
    username;
    email;
    mobile;
    gender;
    address;

    constructor() {}

    setStateName(stateName) {
        this.stateName = stateName;
    }
    setUserName(username) {
        this.username = username;
    }

    setEmail(email) {
        this.email = email;
    }

    setMobile(mobile) {
        this.mobile = mobile;
    }

    setGender(gender) {
        this.gender = gender;
    }

    setAddress(address) {
        this.address = address;
    }
}
like image 374
Vishal Patoliya ツ Avatar asked Jan 16 '19 09:01

Vishal Patoliya ツ


People also ask

How do you send data through navigation in react native?

import { DataNavigation } from 'react-data-navigation'; . . . // For set the data you need to call setData(key, value) Function i.e. // eg. DataNavigation. setData('name', 'Viren'); // it will set the 'Viren' as respect to 'name' key.

How to use the modal component in React Native?

In this chapter, we will show you how to use the modal component in React Native. Let us now create a new file: ModalExample.js We will put logic inside ModalExample. We can update the initial state by running the toggleModal. After updating the initial state by running the toggleModal, we will set the visible property to our modal.

What are React Native’s core components?

You’ve already met React Native’s Core Components. React lets you nest these components inside each other to create new components. These nestable, reusable components are at the heart of the React paradigm. For example, you can nest Text and TextInput inside a View below, and React Native will render them together:

How do I declare the state of a component in react?

Then you declare the component’s state by calling useState inside its function. In this example, useState creates an isHungry state variable: You can use useState to track any kind of data: strings, numbers, Booleans, arrays, objects.

Why can't I use JSX with React Native?

Because JSX is included in the React library, it won’t work if you don’t have import React from 'react' at the top of your file! You’ve already met React Native’s Core Components. React lets you nest these components inside each other to create new components. These nestable, reusable components are at the heart of the React paradigm.


2 Answers

Edit:

After some discussion, this was required answer:

this.props.navigation.navigate('UserList', { userModel: userModel });

this.props.getParam('userModel', /* optional default value */);

I assume this is your UserModel.js. Now you are able to import the model like other components:

import UserModel from './location/UserModel';

But if you do it like this, you'd have to instanciate UserModel every time you import it.

If you'd like to prevent this, just instanciate a UserModel and export it inside the UserModel.js and import the instance anywhere.

Like this:

class UserModel {
    //...
}

export default new UserModel();

other way might be:

export class UserModel {
    //...
}

const GlobalUserModel = new UserModel(); 
export default GlobalUserModel;

to choose in other files what to import:

import { UserModel } from './location/UserModel'; //get new instance

or

import GlobalUserModel from './location/UserModel'; //get global instance

If imported via { UserModel }, you have to instanciate first: new UserModel()

...or vice versa.

like image 36
schogges Avatar answered Oct 15 '22 07:10

schogges


Step 1: Make UserModel.js

class UserModel {

    constructor() {
        stateName,
        username,
        email,
        mobile,
        gender,
        address;
    }

}

Note: Do not Export it if you don't want to set globally.

Step 2 : Screen1.js - Set UserModel and pass from screen1.

_handlePress = async () => {

        UserModel.username = "Vishal Patoliya"

        this.props.navigation.navigate('UserList',{userData: UserModel});

    }

Step 3 : Receiving model class at another screen.

render() {
        console.log(TAG, "render() Called.")

        const UserModel = this.props.navigation.getParam('userData');

        console.log("Username", UserModel.username)
}

OutPut :

01-16 17:30:32.085  4541  5638 I ReactNativeJS: 'Username', 'Vishal Patoliya'
like image 143
Vishal Patoliya ツ Avatar answered Oct 15 '22 05:10

Vishal Patoliya ツ