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;
}
}
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.
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.
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:
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.
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.
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.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With