get element in react native - reactjs

I want to create a Text element inside a View element, how do I link that? I've tried the following. (After typing inside the input, a search is made in the database and the result is translated into a text element).
class SearchScreen extends React.Component {
state = {
inputValue: "",
};
search() {
//Here I do the search in Firebase Realtime Database (it works)
var textElement = React.createElement(
Text,
{ style: { fontSize: 20 } },
[...] //Here inside is the retrieved data from the database
);
var resultView = useRef(resultView); //This doesn't work
ReactDOM.render(textElement, resultView);
}
setSearch = (inputValue) => {
this.setState({ inputValue }, () => this.search());
};
render() {
return (
<View>
<TextInput
onChangeText={(inputValue) => this.setSearch(inputValue)}
value={this.state.inputValue}
/>
<View ref="resultView">
</View>
</View>
)
}
}
export default SearchScreen;

ReactDom doesn't work with React Native.
Try something like this:
class SearchScreen extends React.Component {
state = {
inputValue: '',
resultText: '',
resultList: [],
};
search() {
//Here I do the search in Firebase Realtime Database (it works)
//[...] //Here inside is the retrieved data from the database
// Simulate request to the database
setTimeout(() => {
const databaseResultText = 'Hello World';
this.setState({
resultText: databaseResultText,
});
const databaseResultList = [
{
name: 'Bob',
},
{
name: 'Steve',
},
];
this.setState({
resultList: databaseResultList,
});
}, 1000);
}
setSearch = (inputValue) => {
this.setState({inputValue}, () => this.search());
};
render() {
return (
<View>
<TextInput
onChangeText={(inputValue) => this.setSearch(inputValue)}
value={this.state.inputValue}
/>
<View>
<Text>{this.state.resultText}</Text>
</View>
<View>
{this.state.resultList.map((item) => {
return <Text>{item.name}</Text>;
})}
</View>
</View>
);
}
}
export default SearchScreen;

Related

React native VirtualizedList Re-render while scroll the list

I have Virtualized List initial render record up to 30 ,while render the data list automatically re render 2 to 4 times and also the new data added to the list
while rendering multi times we can't able to do any action like touch or navigate to another screen
My Code
class HomeDetails extends PureComponent {
constructor(props) {
super(props);
this.cellRefs = {};
this.flatListRef = React.createRef();
}
getItem = (data, index) => {
if (index in data) {
return {
key: `${data[index].id} - ${index}`,
id: data[index].id,
accountId: data[index].accountId,
displayName: data[index].displayName,
fullName: data[index].fullName,
};
}
};
keyExtractor(item, index) {
return `${item.id} - ${index}`;
}
getItemCount = data => {
return data.length;
};
_renderItem =({item,index}) => {
console.log(
'Rerendring',
item.accountId,
moment().format('MM/DD/YY hh:mm:ss a'),
);
return (
<View key={index} style={{height: 50, flexDirection: 'row'}}>
<Text>{`${item.accountId} ${moment().format(
'MM/DD/YY hh:mm:ss a',
)}`}</Text>
</View>
);
}
render(){
return (
<VirtualizedList
onScroll={this.onScrollHandler}
onViewableItemsChanged={this._onViewableItemsChanged}
viewabilityConfig={viewabilityConfig}
scrollEventThrottle={16}
ref={this.flatListRef}
horizontal={false}
decelerationRate="normal"
showsHorizontalScrollIndicator={false}
showsVerticalScrollIndicator={false}
data={this.props.responseRecord}
pagingEnabled={true}
scrollToOverflowEnabled={false}
renderItem={this._renderItem}
keyExtractor={this.keyExtractor}
getItemCount={this.getItemCount}
getItem={this.getItem}
windowSize={21}
progressViewOffset={20}
initialNumToRender={15}
maxToRenderPerBatch={15}
updateCellsBatchingPeriod={100}
onEndReached={val => {
return this.props.getExtraData(2, 1);
}}
onEndReachedThreshold={0.1}
refreshing={this.props.postLoading}
extraData={this.props.refreshData}
disableIntervalMomentum={false}
removeClippedSubviews={true}
onRefresh={() => {
return this.props.getExtraData(1, 1);
}}
ItemSeparator={this.ItemSeparator}
ListFooterComponent={this.renderFooter}
/>
)
}
}
const mapStateToProps = ({post, auth, common}) => {
const {
responseRecord,
postLoading,
refreshData,
} = post;
return {
responseRecord,
postLoading,
refreshData,
};
};
const mapDispatchToProps = {
getExtraData,
};
export default connect(mapStateToProps, mapDispatchToProps)(HomeDetails);
..........................................................................
1.For initial 30 record rendering its re-render more that 2 times
2.when add more records its re-render more than 4 to 6 times
3.tried with purecomponent but no luck
code deployed in snack
https://snack.expo.dev/#pandianvpsm/cd5737
Internal, React's PureComponent implements the shouldComponentUpdate method and compares previous props values and new props or state values to avoid unnecessary re-renders.
This works well for primitive type values like numbers, strings, and booleans.
For referential types values (objects and arrays), this comparison is not always accurate. This is the behavior you have. this.props.responseRecord is an array of objects (referential types).
We can solve this problem by implementing our own componentShouldUpdate method as below:
/** Trigger component rerender only new elements added */
shouldComponentUpdate(nextProps, nextState) {
return this.props.responseRecord.length !== nextProps.responseRecord.length
}
Full code below
class HomeDetails extends React.Component {
constructor(props) {
super(props);
this.cellRefs = {};
this.flatListRef = React.createRef();
}
/** Trigger component rerender only new elements added */
shouldComponentUpdate(nextProps, nextState) {
return this.props.responseRecord.length !== nextProps.responseRecord;
}
getItem = (data, index) => {
if (index in data) {
return {
key: `${data[index].id} - ${index}`,
id: data[index].id,
accountId: data[index].accountId,
displayName: data[index].displayName,
fullName: data[index].fullName,
};
}
};
keyExtractor(item, index) {
return `${item.id} - ${index}`;
}
getItemCount = (data) => {
return data.length;
};
_renderItem = ({ item, index }) => {
console.log(
"Rerendring",
item.accountId,
moment().format("MM/DD/YY hh:mm:ss a")
);
return (
<View key={index} style={{ height: 50, flexDirection: "row" }}>
<Text>{`${item.accountId} ${moment().format(
"MM/DD/YY hh:mm:ss a"
)}`}</Text>
</View>
);
};
render() {
return (
<VirtualizedList
onScroll={this.onScrollHandler}
onViewableItemsChanged={this._onViewableItemsChanged}
viewabilityConfig={viewabilityConfig}
scrollEventThrottle={16}
ref={this.flatListRef}
horizontal={false}
decelerationRate="normal"
showsHorizontalScrollIndicator={false}
showsVerticalScrollIndicator={false}
data={this.props.responseRecord}
pagingEnabled={true}
scrollToOverflowEnabled={false}
renderItem={this._renderItem}
keyExtractor={this.keyExtractor}
getItemCount={this.getItemCount}
getItem={this.getItem}
windowSize={21}
progressViewOffset={20}
initialNumToRender={15}
maxToRenderPerBatch={15}
updateCellsBatchingPeriod={100}
onEndReached={(val) => {
return this.props.getExtraData(2, 1);
}}
onEndReachedThreshold={0.1}
refreshing={this.props.postLoading}
extraData={this.props.refreshData}
disableIntervalMomentum={false}
removeClippedSubviews={true}
onRefresh={() => {
return this.props.getExtraData(1, 1);
}}
ItemSeparator={this.ItemSeparator}
ListFooterComponent={this.renderFooter}
/>
);
}
}
const mapStateToProps = ({ post, auth, common }) => {
const { responseRecord, postLoading, refreshData } = post;
return {
responseRecord,
postLoading,
refreshData,
};
};
const mapDispatchToProps = {
getExtraData,
};
export default connect(mapStateToProps, mapDispatchToProps)(HomeDetails);

React state is not updated immediately after deleting data

I have a problem to update the view in React Native after deleting a POST.
I think it could be a problem with the "state" but don't know how to fix it.
This is my list of Posts.
When I press on an item, it ask us to confirm the action.
When we confirm the action of delete, POST is deleted from Firebase but the view is not updated (Still 2 items in the list but only one in database. if I refresh and re-enter to the component, the list is updated)
This is my code :
class GetPosts extends React.Component {
static navigationOptions = ({navigation}) => {
const {params} = navigation.state;
};
constructor(props) {
super(props);
this.state = {
data: {},
data2: [],
posts: {},
newArray: [],
postsCount: 0,
};
}
componentDidMount() {
var f_id = this.props.identifier;
firebase
.database()
.ref('/posts/')
.orderByKey()
.on('value', snapshot => {
snapshot.forEach(el => {
if (el.val().film_id == f_id) {
this.state.data = [
{
email: el.val().email,
puid: el.val().puid,
username: el.val().username,
time: el.val().time,
text: el.val().text,
},
];
this.setState({
data2: this.state.data2.concat(this.state.data),
});
}
});
this.state.data2.forEach(obj => {
if (!this.state.newArray.some(o => o.puid === obj.puid)) {
this.state.newArray.push({...obj});
}
});
this.setState({
posts: this.state.newArray,
postsCount: _.size(this.state.newArray),
});
console.log('valeur finale POSTS=' + this.state.posts);
});
}
renderPosts() {
const postArray = [];
_.forEach(this.state.posts, (value, index) => {
const time = value.time;
const timeString = moment(time).fromNow();
postArray.push(
<TouchableOpacity
onLongPress={this._handleDelete.bind(this, value.puid)}
key={index}>
<PostDesign
posterName={value.username}
postTime={timeString}
postContent={value.text}
/>
</TouchableOpacity>,
);
//console.log(postArray);
});
_.reverse(postArray);
return postArray;
}
_handleDelete(puid) {
const email = firebase.auth().currentUser.email;
let user_email = firebase.database().ref('/posts');
user_email.once('value').then(snapshot => {
snapshot.forEach(el => {
console.log('Userdb :' + el.val().email);
if (email === el.val().email) {
Alert.alert(
'Supprimer le message',
'Are you sure to delete the post?',
[
{text: 'Oui', onPress: () => this._deleteConfirmed(puid)},
{text: 'Non'},
],
);
//console.log('Userdb :' + el.val().email);
} else {
//
console.log('Usercur :' + email);
}
});
});
}
_deleteConfirmed(puid) {
const uid = firebase.auth().currentUser.uid;
firebase
.database()
.ref('/posts/' + uid + puid)
.remove();
this.setState({
posts: this.state.newArray.filter(user => user.puid !== puid),
});
}
render() {
return (
<View style={styles.container}>
<View style={styles.profileInfoContainer}>
<View style={styles.profileNameContainer}>
<Text style={styles.profileName}>{this.props.email}</Text>
</View>
<View style={styles.profileCountsContainer}>
<Text style={styles.profileCounts}>{this.state.postsCount}</Text>
<Text style={styles.countsName}>POSTS</Text>
</View>
</View>
<ScrollView styles={styles.postContainer}>
{this.renderPosts()}
</ScrollView>
</View>
);
}
}
Thank you in advance !!
Several places in your code you are accessing this.state inside of setState, which can cause problems like this. You should be using a function with prevProps whenever you are accessing state within setState.
For example, within _deleteConfirmed:
this.setState({
posts: this.state.newArray.filter(user => user.puid !== puid),
});
should be changed to:
this.setSate(prevState => ({
posts: prevState.newArray.filter(user => user.puid !== puid),
});

How to pass a prop from one component to another component? (React Native)

I've a Custom Component for Tab View through which I can make dynamic tabs and below is the code for it.
TabView is the custom to make those custom tabs and Selected is the component for a single Tab.
How can I send a prop from TabView component to Selected component?
I know how to send props in a regular scenario, but I don't know to send one in this case.
I made this component from the below link:
https://medium.com/#abdelhalim.ahmed95/how-to-create-scrollable-and-dynamic-top-tabsbar-using-react-navigation-17ca52acbc51
export class TabView extends Component {
Tabs = navigation => {
let tabs=['A', 'B', 'C', 'D','E','F','G','H'];
tabs = tabs.reduce((val, tab) => {
val[tab] = {
screen: Selected
}
return val
}, {})
const bottomTabs = createMaterialTopTabNavigator(
{
...tabs
},
{
tabBarOptions: {
style: {
backgroundColor: Themes.colors.FC_PRIMARY,
},
indicatorStyle:{
height: 2,
backgroundColor: Themes.colors.TC_PRIMARY_LIGHT,
},
}
}
)
const Tabs = createAppContainer(bottomTabs);
return <Tabs />
}
render() {
const { navigation } = this.props;
return (
<View style={STYLES.tabView}>
{this.Tabs(navigation)}
</View>
);
}
}
export class Selected extends Component {
constructor(props){
super(props);
this.state = {
screen: '',
screenType: this.props.type
}
}
static navigationOptions = ({ navigation }) => {
return({
tabBarLabel: ({ focused }) => (
<View>
<View style={STYLES.secondaryTabLabel}>
<H3
type={ focused ? "light" : "disabled" }
text={navigation.state.routeName}
/>
</View>
</View>
)
})
};
screenIs = payload => {
this.setState({ screen: payload.state.routeName })
}
render() {
const { navigation } = this.props;
return (
<View style={{flex: 1}}>
<NavigationEvents onWillFocus={this.screenIs} />
<Text>{this.state.screen}</Text>
</View>
);
}
}
Use the following code,
val[tab] = {
screen: () => (<Selected val={val}/>) //in case if you want to send val as props
}
So the final code of yours will be,
export class TabView extends Component {
Tabs = navigation => {
let tabs=['A', 'B', 'C', 'D','E','F','G','H'];
tabs = tabs.reduce((val, tab) => {
val[tab] = {
screen: () => (<Selected val={val}/>), // for props
navigationOptions: {
title: 'Shows' // send anything here to get in navigationOptions
},
}
return val
}, {})
const bottomTabs = createMaterialTopTabNavigator(
{
...tabs
},
{
tabBarOptions: {
style: {
backgroundColor: Themes.colors.FC_PRIMARY,
},
indicatorStyle:{
height: 2,
backgroundColor: Themes.colors.TC_PRIMARY_LIGHT,
},
}
}
)
const Tabs = createAppContainer(bottomTabs);
return <Tabs />
}
render() {
const { navigation } = this.props;
return (
<View style={STYLES.tabView}>
{this.Tabs(navigation)}
</View>
);
}
}
export class Selected extends Component {
constructor(props){
super(props);
this.state = {
screen: '',
screenType: this.props.type
}
}
static navigationOptions = ({ navigation, navigationOptions }) => {
return({
tabBarLabel: ({ focused }) => (
<View>
<View style={STYLES.secondaryTabLabel}>
<H3
type={ focused ? "light" : "disabled" }
text={navigationOptions.title} // use here
/>
</View>
</View>
)
})
};
screenIs = payload => {
this.setState({ screen: payload.state.routeName })
}
render() {
const { navigation } = this.props;
return (
<View style={{flex: 1}}>
<NavigationEvents onWillFocus={this.screenIs} />
<Text>{this.state.screen}</Text>
</View>
);
}
}

Duplicate asynchronous component twice render the last

I create the same component (Recipes) or Screen twice, sending different properties.
<Recipes setRecipes = {this.setRecipes} id="1" />
<Quantity setQuantity = {this.setQuantity} />
<Recipes setRecipes = {this.setRecipes2} id="2" />
<Quantity setQuantity = {this.setQuantity} />
The properties-functions are the following. Only states change.
setRecipes = (recipe) => {
this.setState({recipe:recipe})
}
setRecipes2 = (recipe2) => {
this.setState({recipe2:recipe2})
}
In mty component called Recipes, I get my local database (I use pouchdb), my recipes and products asicronically.
import React, { Component } from 'react';
import { Text, View, TouchableOpacity, ScrollView } from 'react-native';
import style from './Styles/styleNew';
import PouchDB from 'pouchdb-react-native';
PouchDB.plugin(require('pouchdb-find'));
const gThis = null;
const db = new PouchDB('X')
export default class Recipes extends Component {
constructor(props) {
super(props);
gThis = this;
this.state = {
setRecipes: this.props.setRecipes,
id: this.props.id,
recipeID: null,
options: [ ],
};
}
getRecipes() {
let data = []
db.find({
selector: {
type: {
"$eq": this.state.id,
},
owner: {
"$in": ['user']
},
},
}).then(function (recipes) {
recipes = recipes.docs
for (let i = 0; i < recipes.length; i++) {
recipe = recipes[i]
let current = {
id: recipe._id,
name: recipe.name,
recipe: recipe,
}
data.push(current)
}
gThis.setState({ options: data })
}).catch(function (err) {
console.warn(err.toString())
});
}
componentDidMount() {
this.getRecipes()
}
handleBackPress = () => {
this.props.navigation.goBack();
}
defineRecipe(recipe, index) {
this.setState({ recipeID: recipe._id });
this.state.setRecipes(recipe)
}
render() {
return (
<View style={{ flex: 1 }}>
<Text style={style.listText}>RECIPES {this.state.id} </Text>
<ScrollView style={style.listScroll}>
{this.state.options.length >0 &&
this.state.options.map((item, index) => {
key = index + '-' + this.state.id
//key = new Date().toISOString()
console.log(key)
return (
<TouchableOpacity
style={this.state.recipeID === item.id ? style.listOptionSelected : style.listOption}
onPress={() => { this.defineRecipe(item.recipe, index);}}
key={key} >
<Text style={style.listOptionText}>{item.name}</Text>
</TouchableOpacity>
);
})
}
</ScrollView>
</View>
);
}
}
But the second Screen Recipe is rendered twice.
Render 1
Render 2
As you can see, the content is replaced.
I think it might have something to do with you gThis which I assume is a global this?
A better way to try and access this inside your class members is to either use lexical arrow functions in your class like this:
getRecipes = () => {
// implementation
}
Or bind your class members to this in the constructor as follows
constructor(props) {
super(props);
gThis = this;
this.state = {
id: this.props.id,
recipeID: null,
options: [ ],
};
this.getRecipes = this.getRecipes.bind(this);
this.defineRecipe = this.defineRecipe.bind(this);
}
On a different node, you have babel transpiling your ES6 code from as far as I can see.
I would strongly recommend replacing your for loops with es6 variants such as a map or forEach for readability
const options = recipes.map(recipe => {
const { _id: id, name } = recipe;
data.push({
id,
name,
recipe,
});
});
this.setState({ options });

react native send props to components when declare object

I'm trying to send into my componentsObject in FooScreen any props and to use it into the components, but it not let me use it.
const FooScreen = ({props}) => <Center><Text>{props}</Text></Center>;
const BarScreen = () => <Center><Text>Bar</Text></Center>;
const components = {
Foo: FooScreen({name:'test1'}),
Bar: BarScreen({name:'test2'}),
};
const Center = ({ children }) => (
<View style={{ alignItems: 'center', justifyContent: 'center', flex: 1 }}>{children}</View>
);
const pages = [
{ screenName: 'Foo', componentName: 'Foo' },
{ screenName: 'Bar', componentName: 'Bar' },
];
i send it as props in Screen and in other screen i try to use it as
class TabBarView extends Component {
constructor(props){
super(props);
this.state = {
tabs: ''
}
}
componentDidMount(){
console.log(this.props)
}
componentWillMount(){
console.log(this.props)
const {pages,components} = this.props
setTimeout(() => {
const screens = {};
pages.forEach(page => {
screens[page.screenName] = { screen: components[page.componentName] };
});
this.setState({ tabs: TabNavigator(screens) });
}, 2000);
}
render() {
if (this.state.tabs) {
return <this.state.tabs />;
}
return <View><Text>Loading...</Text></View>;
}
}
it fail and not let me do that.
later, I want to use in FooScreen as real screen in react and set it into stackNavigator
I get the error
The component for route 'Foo' must be a react component
I suggest the component returns function instead of React element. It's easy to assign a key for each element.
The setState should not be used in componentWillMount, especially when there is a timer to cause side-effect.
For efficiency reason, I tested the code below on web. If you replace div with View and p with Text, this should work in React Native. Don't forget import { Text, View } from 'react-native'
import React, { Component } from 'react';
const FooScreen = props => (
<Center>
<Text>{`[Foo] ${props.name}`}</Text>
</Center>
);
const BarScreen = props => (
<Center>
<Text>{`[Bar] ${props.name}`}</Text>
</Center>
);
const components = {
Foo: (key) => <FooScreen name="test1" key={key} />,
Bar: (key) => <BarScreen name="test2" key={key} />,
};
const Center = props => (
<View style={{ alignItems: 'center', justifyContent: 'center', flex: 1 }}>
{props.children}
</View>
);
const pages = [ 'Foo', 'Bar' ];
export default class TabBardiv extends Component {
state = {
tabs: null,
};
componentDidMount() {
console.log(pages);
setTimeout(() => {
this.setState({ tabs: pages });
}, 2000);
}
render() {
if (!this.state.tabs) {
return (
<View>
<Text>Loading...</Text>
</View>
);
}
const screens = pages.map((page, index) => {
const element = components[page];
console.log(element);
return element(index);
});
return screens;
}
}

Resources