React-image-Gallery dependency giving error - javascript

https://i.stack.imgur.com/bkGNA.png
I am getting error as shown in the image, when i trigger the ImageGallery component in my code. I am passing image items as props to ImageGallery and displaying Imagegallery on pop-up modal. But i am not able to display images on pop-up modal due to error i am getting as shown in the image
import React, {Component} from 'react'
import {Modal,Button} from '../../../components/elements'
import ImageGallery from 'react-image-gallery';
import "react-image-gallery/styles/css/image-gallery.css";
class ProfileImageGridModal extends Component {
constructor(props) {
super(props)
this.state = {
showGridModal: false,
images : [
{
original: 'https://cdn.pixabay.com/photo/2016/03/27/17/59/motorcycle-1283299_960_720.jpg',
thumbnail: 'https://cdn.pixabay.com/photo/2016/03/27/17/59/motorcycle-1283299_960_720.jpg',
},
{
original: 'https://cdn.pixabay.com/photo/2016/11/29/10/21/dirt-bike-1868996_960_720.jpg',
thumbnail: 'https://cdn.pixabay.com/photo/2016/11/29/10/21/dirt-bike-1868996_960_720.jpg',
},
{
original: 'https://cdn.pixabay.com/photo/2019/05/08/01/52/motorcycle-4187586_960_720.jpg',
thumbnail: 'https://cdn.pixabay.com/photo/2019/05/08/01/52/motorcycle-4187586_960_720.jpg',
},
{
original: 'https://cdn.pixabay.com/photo/2014/04/23/20/34/dirt-bike-330815_960_720.jpg',
thumbnail: 'https://cdn.pixabay.com/photo/2014/04/23/20/34/dirt-bike-330815_960_720.jpg',
},
{
original: 'https://cdn.pixabay.com/photo/2021/10/17/18/55/motorcycle-6719182_960_720.jpg',
thumbnail: 'https://cdn.pixabay.com/photo/2021/10/17/18/55/motorcycle-6719182_960_720.jpg',
},
{
original: 'https://cdn.pixabay.com/photo/2021/10/17/18/55/motorcycle-6719182_960_720.jpg',
thumbnail: 'https://cdn.pixabay.com/photo/2021/10/17/18/55/motorcycle-6719182_960_720.jpg',
},
{
original: 'https://cdn.pixabay.com/photo/2021/10/17/18/55/motorcycle-6719182_960_720.jpg',
thumbnail: 'https://cdn.pixabay.com/photo/2021/10/17/18/55/motorcycle-6719182_960_720.jpg',
},
{
original: 'https://cdn.pixabay.com/photo/2021/10/17/18/55/motorcycle-6719182_960_720.jpg',
thumbnail: 'https://cdn.pixabay.com/photo/2021/10/17/18/55/motorcycle-6719182_960_720.jpg',
},
{
original: 'https://cdn.pixabay.com/photo/2021/10/17/18/55/motorcycle-6719182_960_720.jpg',
thumbnail: 'https://cdn.pixabay.com/photo/2021/10/17/18/55/motorcycle-6719182_960_720.jpg',
},
],
renderRightNav : (onClick, disabled) => (
<RightNav onClick={onClick} disabled={disabled} />
)
,
renderLeftNav : (onClick, disabled) => (
<LeftNav onClick={onClick} disabled={disabled} />
)
}
}
render() {
const { isMobile, isReviewModal } = this.props
return (
<div className="see-more-wrapper">
{ !isReviewModal ?
<Button
className="see-more-gallery-btn"
onClick={() => {
document.body.classList.add('bike-profile-modal-open')
this.setState({ showGridModal: true })
}}
>
<i class="fa fa-th" aria-hidden="true"></i> See All 10 Photos
</Button>:
<div className="review-see-more">
<Button
className="review-see-more-gallery-btn"
onClick={() => {
document.body.classList.add('bike-profile-modal-open')
this.setState({ showGridModal: true })
}}
>
<i class="fa fa-th" aria-hidden="true"></i> <br/>See All <br/>10 Photos
</Button>
</div>
}
{ this.state.showGridModal &&
<div className="grid-gallery-modal-wrapper">
<Modal
className="my-modal-name"
showClose={true}
closeOnEsc={true}
closeOnBlur={true}
onClose={() => {
document.body.classList.remove('bike-profile-modal-open')
this.setState({ showGridModal: false })
}}
show={this.state.showGridModal}
size="fullscreen"
>
<ImageGallery
items={this.state.images}
renderRightNav = { this.state.renderRightNav}
renderLeftNav = { this.state.renderLeftNav}
/>
</Modal>
</div>
}
</div>
);
}
}
const RightNav = (props) => {
const settings = {
width: "3rem",
height: "3rem",
borderRadius: "50%",
background: "#AF985F",
fontStyle: "normal",
fontWeight: "bold",
fontSize: "24px",
lineHeight: "29px",
color:"#FFFFFF",
display: "flex",
alignItems: "center",
justifyContent: "center",
cursor: "pointer"
}
const { onClick } = props
return(
<button className="bikegallery-nextarrow-wrapper image-gallery-icon image-gallery-right-nav" onClick={onClick}>
<div classname="next-arrow">
<i class="fas fa-arrow-right" style={settings}></i>
</div>
</button>
)
}
const LeftNav = (props) => {
const settings = {
width: "3rem",
height: "3rem",
borderRadius: "50%",
background: "#AF985F",
fontStyle: "normal",
fontWeight: "bold",
fontSize: "24px",
lineHeight: "29px",
color:"#FFFFFF",
display: "flex",
alignItems: "center",
justifyContent: "center",
cursor: "pointer"
}
const { onClick } = props
return(
<button className="bikegallery-leftarrow-wrapper image-gallery-icon image-gallery-left-nav" onClick={onClick}>
<div classname="next-arrow">
<i class="fas fa-arrow-left" style={settings}></i>
</div>
</button>
)
}
export default ProfileImageGridModal
ProfileImageGridModal.defaultProps = {
showGridModal: false
}

You are using hooks wrongly. This is not React-image-Gallery error.

Related

TabBar with floating action button in React Native

I was following this tutorial where they created a TaBar with a Floating Button, and after adding the code, it seems to work really well on iOS, as shown below:
On Android, it is a different case; it appears the floating button gets cropped right where the BottomBar ends, as shown below:
I do not understand why this is happening, below is the code
import React from "react";
import { StyleSheet, View } from "react-native";
import {
BottomTabBar,
createBottomTabNavigator,
} from "#react-navigation/bottom-tabs";
import { FontAwesome as Icon } from "#expo/vector-icons";
import { TabBarAdvancedButton } from "./TabBarAdvancedButton";
import { isIphoneX } from "src/utils/deviceHelpers";
import { DARKTEXT, WHITE } from "src/styles/colors";
import { BlankScreen } from "src/screens";
import { Badge } from "react-native-paper";
const BottomBar = createBottomTabNavigator();
type Props = {
barColor?: string;
isBtnDisabled?: boolean;
hasNotification?: boolean;
};
export const TabBar: React.FC<Props> = ({
barColor = WHITE,
isBtnDisabled = false,
hasNotification = false,
...props
}) => {
return (
<BottomBar.Navigator
backBehaviour="initialRoute"
name="BarStack"
tabBar={(props) => (
<View style={styles.navigatorContainer}>
<BottomTabBar {...props} />
{isIphoneX() && (
<View
style={[
styles.xFillLine,
{
backgroundColor: barColor,
},
]}
/>
)}
</View>
)}
tabBarOptions={{
showIcon: true,
style: styles.navigator,
tabStyle: {
backgroundColor: barColor,
},
}}
>
<BottomBar.Screen
name="Home"
component={BlankScreen}
options={{
tabBarIcon: ({ color }) => (
<Icon name="home" size={24} color={color} />
),
}}
/>
<BottomBar.Screen
name="Lists"
component={BlankScreen}
options={{
tabBarIcon: ({ color }) => (
<Icon name="file" size={24} color={color} />
),
}}
/>
<BottomBar.Screen
name="Rocket"
component={BlankScreen}
options={{
tabBarButton: (props) => (
<TabBarAdvancedButton bgColor={barColor} {...props} />
),
}}
/>
<BottomBar.Screen
name="Alerts"
component={BlankScreen}
options={{
tabBarIcon: ({ color }) => (
<View>
<Icon name="bell" size={24} color={color} />
{hasNotification && <Badge />}
</View>
),
}}
/>
<BottomBar.Screen
name="search"
component={BlankScreen}
options={{
tabBarIcon: ({ color }) => (
<Icon name="search" size={24} color={color} />
),
}}
/>
</BottomBar.Navigator>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
},
navigatorContainer: {
position: "absolute",
bottom: 0,
left: 0,
right: 0,
shadowColor: "#000",
shadowOffset: {
width: 0,
height: 1,
},
shadowOpacity: 0.22,
shadowRadius: 2.22,
},
navigator: {
borderTopWidth: 0,
backgroundColor: "transparent",
elevation: 30,
},
tabIconStyle: {
color: DARKTEXT,
},
xFillLine: {
position: "absolute",
bottom: 0,
left: 0,
right: 0,
height: 34,
},
});
export default TabBar;
And the Floating button:
import React from 'react';
import { StyleSheet, TouchableOpacity, View } from 'react-native';
import { BottomTabBarButtonProps } from '#react-navigation/bottom-tabs/lib/typescript/src/types';
import { FontAwesome as Icon } from '#expo/vector-icons';
import { TabBg } from './svg';
type Props = BottomTabBarButtonProps & {
bgColor?: string;
};
export const TabBarAdvancedButton: React.FC<Props> = ({
bgColor,
...props
}) => (
<View
style={styles.container}
pointerEvents="box-none"
>
<TabBg
color={bgColor}
style={styles.background}
/>
<TouchableOpacity
style={styles.button}
onPress={props.onPress}
>
<Icon
name="rocket"
style={styles.buttonIcon}
/>
</TouchableOpacity>
</View>
);
const styles = StyleSheet.create({
container: {
position: 'relative',
width: 75,
alignItems: 'center',
},
background: {
position: 'absolute',
top: 0,
},
button: {
top: -22.5,
justifyContent: 'center',
alignItems: 'center',
width: 50,
height: 50,
borderRadius: 27,
backgroundColor: '#E94F37',
},
buttonIcon: {
fontSize: 16,
color: '#F6F7EB'
}
});

how to change div background color by using onMouseEnter only on currently moused over element in React

I am making a basic drop down menu in React. I am new to React and have managed to get to where the menu opens on click and such. What I would like to do is change the background of each choice to a different color only when the mouse is hovered over that specific element.
Right now the issue is that every element is highlighted when one is hovered over. I understand this is because I am changing the state which effects every element, but I cant wrap my mind around how to implement it how I would like it. Perhaps there is a React concept(s) I am unaware of that would make this easier.
Here is my code so far:
App.jsx file
import React from "react";
import InnerDiv from './components/InnerDiv';
import CountiesList from "./components/countiesList";
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
menuIsOpen: false,
height: '50px'
}
this.handleClick = this.handleClick.bind(this)
}
handleClick = () => {
if (this.state.menuIsOpen == false) {
this.setState(state => ({
menuIsOpen: !state.menuIsOpen,
height: '350px'
}))
} else {
this.setState(state => ({
menuIsOpen: !state.menuIsOpen,
height: '50px'
}))
}
}
render() {
const divStyle = {
height: this.state.height,
width: '300px',
border: '2px solid cornflowerblue',
borderRadius: '12px',
position: 'absolute',
top: '50px',
left: '50px',
cursor: 'pointer',
display: 'flex',
flexDirection: 'column',
justifyContent: 'flex-start',
alignItems: 'center',
transition: 'all 0.1s linear',
}
return (
<div onClick={this.handleClick} style={divStyle} className="App">
<InnerDiv/>
<CountiesList/>
</div>
);
}
}
export default App;
InnerDiv.jsx file
import React from "react";
const innerDivStyle = {
height: '50px',
width: '90%',
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between'
}
const h1Style = {
fontSize: '20px',
WebkitTouchCallout: 'none',
WebkitUserSelect: 'none',
khtmlUserSelect: 'none',
MozUserSelect: 'none',
msUserSelect: 'none',
UserSelect: 'none'
}
const svgStyle = {
height: '30px'
}
class InnerDiv extends React.Component {
constructor(props) {
super(props);
}
render(){
return (
<div style={innerDivStyle} className="InnerDiv">
<h1 style={h1Style}>Select A Location</h1>
<svg style={svgStyle} xmlns="http://www.w3.org/2000/svg" id="Layer_1" data-name="Layer 1" viewBox="0 0 122.88 122.88"><title>round-line-bottom-arrow</title><path d="M122.85,61.45h0A61.39,61.39,0,0,0,61.45,0V0h0V0A61.38,61.38,0,0,0,0,61.43H0v0H0a61.35,61.35,0,0,0,61.4,61.38v0h0v0a61.34,61.34,0,0,0,61.38-61.4ZM61.44,91,33.92,60.47H51.5V39.2H71.38V60.47H89L61.44,91Zm48.28-29.54h0a48.36,48.36,0,0,1-48.27,48.29v0h0v0A48.35,48.35,0,0,1,13.14,61.47h0v0h0A48.27,48.27,0,0,1,61.41,13.14v0h0v0a48.3,48.3,0,0,1,48.27,48.3Z"/></svg>
</div>
)
}
}
export default InnerDiv;
countiesList.jsx file
import React from "react";
class CountiesList extends React.Component {
constructor(props) {
super(props);
this.state = {
counties: [
'Antrim',
'Armagh',
'Carlow',
'Cavan',
'Clare',
'Cork',
'Derry',
'Donegal',
'Down',
'Dublin',
'Fermanagh',
'Galway',
'Kerry',
'Kildare',
'Kilkenny',
'Laois',
'Leitrim',
'Limerick',
'Longford',
'Louth',
'Mayo',
'Meath',
'Monaghan',
'Offaly',
'Roscommon',
'Sligo',
'Tipperary',
'Tyrone',
'Waterford',
'Westmeath',
'Wexford',
'Wicklow'
],
backgroundColor: '#a9c4f5'
};
this.updateBackgroundColor = this.updateBackgroundColor.bind(this);
this.reverseBackgroundColor = this.reverseBackgroundColor.bind(this);
}
updateBackgroundColor() {
this.setState({backgroundColor: 'cornflowerblue'})
}
reverseBackgroundColor() {
this.setState({backgroundColor: '#a9c4f5'})
}
render() {
const ulStyle = {
listStyleType: 'none',
paddingInlineStart: 0,
margin: 0,
width: '100%',
height: '300px',
overflowY: 'scroll',
borderBottomLeftRadius: '12px'
}
const liItemContainer = {
height: '50px',
paddingLeft: '15px',
display: 'flex',
alignItems: 'center',
background: this.state.backgroundColor,
}
const liStyle = {
fontWeight: '700'
}
let countiesListItems = this.state.counties.map(county => {
return (
<div key={county} style={liItemContainer} onMouseEnter={this.updateBackgroundColor} onMouseOut={this.reverseBackgroundColor}>
<li style={liStyle}>{county}</li>
</div>
)
})
return (
<ul style={ulStyle}>
{countiesListItems}
</ul>
)
}
}
export default CountiesList;
Thank you for your help in advance

OnPress that redirect to other page

I am trying to develop an application to learn react native and I am stuck with the login/register.
I have a register.js that works properly, as I have tested it before, and I wanted to add a resiter button to redirect to the register page from the login one.
From the UserLogin.js, the button is this one:
<TouchableOpacity
onPress={() => navigate('Register')}
style={{width:200,padding:10,backgroundColor:'pink',alignItems:'center'}}>
<Text style={{color:'black'}}>Register</Text>
</TouchableOpacity>
UserLogin.js
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,TouchableOpacity,TextInput,Button,Keyboard
} from 'react-native';
import { StackNavigator } from 'react-navigation';
class UserLogin extends React.Component {
constructor(props){
super(props)
this.state={
userEmail:'',
userPassword:''
}
}
login = () =>{
const {userEmail,userPassword} = this.state;
let reg = /^\w+([\.-]?\w+)*#\w+([\.-]?\w+)*(\.\w{2,3})+$/ ;
if(userEmail==""){
//alert("Please enter Email address");
this.setState({email:'Please enter Email address'})
}
else if(reg.test(userEmail) === false)
{
//alert("Email is Not Correct");
this.setState({email:'Email is Not Correct'})
return false;
}
else if(userPassword==""){
this.setState({email:'Please enter password'})
}
else{
fetch('http://192.168.1.47/login.php',{
method:'post',
header:{
'Accept': 'application/json',
'Content-type': 'application/json'
},
body:JSON.stringify({
// we will pass our input data to server
email: userEmail,
password: userPassword
})
})
.then((response) => response.json())
.then((responseJson)=>{
if(responseJson == "ok"){
// redirect to profile page
alert("Successfully Login");
//this.props.navigation.navigate("Profile");
}else{
alert("Wrong Login Details");
}
})
.catch((error)=>{
console.error(error);
});
}
Keyboard.dismiss();
}
render() {
const { navigate } = this.props.navigation;
return (
<View style={styles.container}>
<Text style={{padding:10,margin:10,color:'red'}}>{this.state.email}</Text>
<TextInput
placeholder="Enter Email"
style={{width:200, margin:10}}
onChangeText={userEmail => this.setState({userEmail})}
/>
<TextInput
placeholder="Enter Password"
style={{width:200, margin:10}}
onChangeText={userPassword => this.setState({userPassword})}
/>
<TouchableOpacity
onPress={this.login}
style={{width:200,padding:10,backgroundColor:'magenta',alignItems:'center'}}>
<Text style={{color:'white'}}>Login</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={() => navigate('Register')}
style={{width:200,padding:10,backgroundColor:'pink',alignItems:'center'}}>
<Text style={{color:'black'}}>Register</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
});
export default UserLogin
AppRegistry.registerComponent('login', () => login);
register.js
import React, { Component } from 'react';
import { View, Text, StyleSheet, TextInput, TouchableOpacity, ActivityIndicator, Platform } from 'react-native';
class Register extends React.Component
{
constructor()
{
super();
this.state = { first_name: '', last_name: '', email: '', password: '', loading: false, disabled: false }
}
saveData = () =>
{
this.setState({ loading: true, disabled: true }, () =>
{
fetch('http://192.168.1.47/user_registration.php',
{
method: 'POST',
headers:
{
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(
{
first_name: this.state.first_name,
last_name: this.state.last_name,
email: this.state.email,
password: this.state.password,
})
}).then((response) => response.json()).then((responseJson) =>
{
//Faire une redirection evrs le profile de l'utilisateur
alert(responseJson);
this.setState({ loading: false, disabled: false });
}).catch((error) =>
{
console.error(error);
this.setState({ loading: false, disabled: false });
});
});
}
render()
{
return(
<View style = { styles.container }>
<TextInput underlineColorAndroid = "transparent" placeholder = "Your First Name" style = { styles.textInput } onChangeText = {(text) => this.setState({ first_name: text })}/>
<TextInput underlineColorAndroid = "transparent" placeholder = "Your Last Name" style = { styles.textInput } onChangeText = {(text) => this.setState({ last_name: text })}/>
<TextInput underlineColorAndroid = "transparent" placeholder = "Your Email adress" style = { styles.textInput } onChangeText = {(text) => this.setState({ email: text })}/>
<TextInput underlineColorAndroid = "transparent" placeholder = "Your Password" style = { styles.textInput } onChangeText = {(text) => this.setState({ password: text })}/>
<TouchableOpacity disabled = { this.state.disabled } activeOpacity = { 0.8 } style = { styles.Btn } onPress = { this.saveData }>
<Text style = { styles.btnText }>Sign up</Text>
</TouchableOpacity>
{
(this.state.loading)
?
(<ActivityIndicator size = "large" />)
:
null
}
</View>
);
}
}
const styles = StyleSheet.create(
{
container:
{
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#eee',
paddingHorizontal: 25,
paddingTop: (Platform.OS == 'ios') ? 20 : 0
},
textInput:
{
height: 40,
borderWidth: 1,
borderColor: 'grey',
marginVertical: 5,
alignSelf: 'stretch',
padding: 8,
fontSize: 16
},
Btn:
{
backgroundColor: 'rgba(0,0,0,0.6)',
alignSelf: 'stretch',
padding: 10,
marginTop: 10,
marginBottom: 25
},
btnText:
{
textAlign: 'center',
color: 'white',
fontSize: 16
}
});
export default Register
Navigation.js
import Search from '../Components/Search';
import ProductDetail from '../Components/ProductDetail';
//import UserAccount from '../Components/UserAccount';
import UserLogin from '../Components/UserLogin';
//import UserRegistration from '../Components/UserRegistration';
import Transaction from '../Components/Transaction';
import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
import React from 'react';
import { Image , Text, TouchableOpacity } from 'react-native';
const SearchStackNavigator = createStackNavigator({
Rechercher : {
screen: Search,
navigationOptions: ({ navigate, navigation }) => ({
title:'Rechercher',
headerTitleStyle: {
fontWeight: 'bold',
color: 'white',
marginLeft:5,
},
headerStyle: {
backgroundColor: '#FF6100',
},
headerLeft: (
<TouchableOpacity
onPress={() => navigation.navigate('Rechercher')}>
<Image
style={{
width: 35,
height: 35,
marginLeft: 25,
}}
source={require('../Images/logo.jpg')}
/>
</TouchableOpacity>
),
headerRight: (
<TouchableOpacity
onPress={() => navigation.navigate('Account')}>
<Image
style={{
width: 35,
height: 35,
marginRight: 25,
}}
source={require('../Images/user.png')}
/>
</TouchableOpacity>
)
})
},
ProductDetail : {
screen: ProductDetail,
navigationOptions: ({ navigate, navigation }) => ({
title:'Détail du produit',
headerTitleStyle: {
fontWeight: 'bold',
color: 'white',
marginLeft:5,
},
headerStyle: {
backgroundColor: '#FF6100',
},
headerLeft: (
<TouchableOpacity
onPress={() => navigation.navigate('Rechercher')}>
<Image
style={{
width: 35,
height: 35,
marginLeft: 25,
}}
source={require('../Images/logo.jpg')}
/>
</TouchableOpacity>
),
headerRight: (
<TouchableOpacity
onPress={() => navigation.navigate('Account')}>
<Image
style={{
width: 35,
height: 35,
marginRight: 25,
}}
source={require('../Images/user.png')}
/>
</TouchableOpacity>
)
})
},
Account: {
//screen: UserRegistration,
screen: UserLogin,
navigationOptions: ({ navigate, navigation }) => ({
title:'Compte utilisateur',
headerTitleStyle: {
fontWeight: 'bold',
color: 'white',
marginLeft:5,
},
headerStyle: {
backgroundColor: '#FF6100',
},
headerLeft: (
<TouchableOpacity
onPress={() => navigation.navigate('Rechercher')}>
<Image
style={{
width: 35,
height: 35,
marginLeft: 25,
}}
source={require('../Images/logo.jpg')}
/>
</TouchableOpacity>
),
headerRight: (
<TouchableOpacity
onPress={() => navigation.navigate('Account')}>
<Image
style={{
width: 35,
height: 35,
marginRight: 25,
}}
source={require('../Images/user.png')}
/>
</TouchableOpacity>
)
})
},
Transaction: {
screen: Transaction,
navigationOptions: ({ navigate, navigation }) => ({
title:'Transaction',
headerTitleStyle: {
fontWeight: 'bold',
color: 'white',
marginLeft:5,
},
headerStyle: {
backgroundColor: '#FF6100',
},
headerLeft: (
<TouchableOpacity
onPress={() => navigation.navigate('Rechercher')}>
<Image
style={{
width: 35,
height: 35,
marginLeft: 25,
}}
source={require('../Images/logo.jpg')}
/>
</TouchableOpacity>
),
headerRight: (
<TouchableOpacity
onPress={() => navigation.navigate('Account')}>
<Image
style={{
width: 35,
height: 35,
marginRight: 25,
}}
source={require('../Images/user.png')}
/>
</TouchableOpacity>
)
})
},
})
export default createAppContainer(SearchStackNavigator)
Navigation.js is located on a folder named Navigation.
The two other files are on the same folder (The components one).
Can someone help me fixe it ? I don't think it's complicated, but there is something I didn't understand, I think.
Thank you in advance.
Noémie.
Your Register, and Login screens have to be listed as routes in your
stack-navigation ... so you can get access to navigation object to use
it for navigation – Hend El-Sahli

when I use "useCallback " I get a TypeError error

I have const experience that creates 6 experiences with there popover. I am supposed to add useCallback to it but when I go I get and error.
This is my component experience
import React, { memo, useCallback, useState } from 'react';
import PropTypes from 'prop-types';
import { makeStyles } from '#material-ui/styles';
import Typography from '#material-ui/core/Typography';
import clsx from 'clsx';
import Button from '#material-ui/core/Button';
import Popover from '#material-ui/core/Popover';
import gastronomia from 'assets/experiences/gastronomia.jpg';
import productos from 'assets/experiences/productos.jpg';
import giftcard from 'assets/experiences/giftcard.jpg';
import diversion from 'assets/experiences/diversion.jpg';
import deporte from 'assets/experiences/deporte.jpg';
import belleza from 'assets/experiences/belleza.jpg';
import gastronomiaExperiences from 'data/gastronomia';
import productosExperiences from 'data/productos';
import giftcardExperiences from 'data/giftcard';
import diversionExperiences from 'data/diversion';
import deporteExperiences from 'data/deporte';
import bellezaExperiences from 'data/belleza';
// Proptypes definitions to the component.
const propTypes = {
/** Custom root className. */
className: PropTypes.string,
};
// Default props definitions.
const defaultProps = {
className: null,
};
// Component's styles
const useStyles = makeStyles(theme => ({
root: {
display: 'block',
margin: '0 auto',
maxWidth: '50%',
[theme.breakpoints.down('md')]: {
maxWidth: '70%',
},
[theme.breakpoints.down('sm')]: {
maxWidth: '100%',
},
'& .experiences-column': {
display: 'inline-block',
verticalAlign: 'top',
textAlign: 'center',
'&.col1': {
width: '36.31%',
[theme.breakpoints.down('sm')]: {
width: 'initial',
},
},
'&.col2': {
width: '63.69%',
[theme.breakpoints.down('sm')]: {
width: 'initial',
},
},
'& .experience': {
padding: 2,
position: 'relative',
'& img': {
width: '100%',
display: 'block',
},
'& .experience-title': {
position: 'absolute',
bottom: 30,
left: 0,
right: 0,
textAlign: 'center',
},
},
},
},
paper: {
width: '50%',
left: '25% !important',
height: '280px',
'& img': {
width: '100px',
padding: '0 10px 0 10px',
},
},
gastronomia: {
backgroundColor: 'rgba(0,185,208,0.9)',
},
giftcard: {
backgroundColor: 'rgba(221,165,174,0.9)',
},
deporte: {
backgroundColor: 'rgba(189,143,205,0.9)',
},
productos: {
backgroundColor: 'rgba(221,165,174,0.9)',
},
diversion: {
backgroundColor: 'rgba(255,176,10,0.9)',
},
belleza: {
backgroundColor: 'rgba(229,166,187,0.9)',
},
'#media screen and (max-width: 1024px)': {
paper: {
width: '70%',
left: '15% !important',
},
},
'#media screen and (max-width: 480px)': {
paper: {
width: '100%',
left: '0% !important',
height: '350px',
},
},
}), { name: 'ExperiencesStyle' });
*/const Experiences = memo(
(props) => {
const { className } = props;
const classes = useStyles(props);
const [anchorEl, setAnchorEl] = useState(null);
const handleClick = (event) => {
setAnchorEl(anchorEl ? null : event.currentTarget);
};
const handleClose = () => {
setAnchorEl(null);
};
// const open = Boolean(anchorEl);
const experience = useCallback((img, title, id, popoverCategory, anchorEl, classes, handleClick) => (
<div
className="experience"
aria-describedby={id}
id={id}
onClick={handleClick}
onKeyDown={handleClick}
role="button"
tabIndex="0"
>
<img
data-sizes="auto"
className="lazyload"
data-src={img}
alt={title}
/>
<div className="experience-title">
<Typography
color="textSecondary"
variant="subtitle2"
className="highlight highlight1"
display="inline"
>
{ title }
</Typography>
</div>
<Popover
id={id}
open={anchorEl && anchorEl.id === id}
anchorEl={anchorEl}
onClose={handleClose}
classes={{paper: clsx(classes.paper, classes[id])}}
>
<div>
<Button onClickAway={handleClose}>x</Button>
<div>
{
popoverCategory.map(url => (
<img
key={url}
data-sizes="auto"
className="lazyload"
src={url}
alt={title}
/>
))
}
</div>
</div>
</Popover>
</div>
), []);
return (
<div className={clsx(classes.root, className)}>
<div className="experiences-column col1">
{experience(gastronomia, 'GASTRONOMÍA', 'gastronomia', gastronomiaExperiences)}
{experience(giftcard, 'GIFT CARD', 'giftcard', giftcardExperiences)}
{experience(deporte, 'DEPORTE', 'deporte', deporteExperiences)}
</div>
<div className="experiences-column col2">
{experience(productos, 'PRODUCTOS', 'productos', productosExperiences)}
{experience(diversion, 'DIVERSIÓN', 'diversion', diversionExperiences)}
{experience(belleza, 'BELLEZA', 'belleza', bellezaExperiences)}
</div>
</div>
);
},
);
and the error is:
TypeError: Cannot read property 'paper' of undefined
referring to this line
classes={{paper: clsx(classes.paper, classes[id])}}
where I add the classes to the paper class of the popover.
I am not used to useCallback and new to react so I am lost.
const experience = useCallback((img, title, id, popoverCategory, anchorEl, classes, handleClick) => (
The function you have created expects 7 things to be passed into it. But when you use it, you only pass in 4:
experience(gastronomia, 'GASTRONOMÍA', 'gastronomia', gastronomiaExperiences)
So the remaining 3 are all undefined inside the function. the anchorEl, classes, and handleClick variables defined at the top of your component are not visible inside experience, because those variable are being "shadowed".
So you could stop shadowing the variables by simply removing the last 3 arguments from your function definition:
const experience = useCallback((img, title, id, popoverCategory) => (
However, i do have to express that useCallback doesn't seem to be doing anything for you. The benefit of useCallback is that you can have the experience variable be the same reference from one render to the next, but that doesn't seem to be a feature that you need. Your component never tries to compare references of experience, nor is experience passed to any other component where it might be checked in a shouldComponentUpdate or React.memo.
So i would recommend deleting the useCallback entirely:
const experience = (image, title, id, popoverCategory) => (

Highlighting menu item when scrolled to certain section in React.js

Cannot solve that one.
I'm creating navigation bar with a scroll-to functionality, ie. when pressed on the certain menu item the page is scrolled to the corresponding section. While this seems to be quite straightforward I don't know how to highlight in a different colour this certain menu item when window gets to the point of the section. F.ex. user presses 'contact', the page is scrolled to contact section, the contact menu item changes its colour to red. Thank you for any help!
Here's my code:
import React, { Component } from 'react';
import Container from './Container.jsx';
class Header extends Component {
constructor(props) {
super(props);
this.state = {
backgroundColor: 'rgba(34,34,34,0)',
};
}
componentDidMount() {
window.addEventListener('scroll', () => this.handleScroll());
}
handleScroll(e) {
const test = (pageYOffset > 900) ?
(this.setState({ backgroundColor: 'black' })) :
(this.setState({ backgroundColor: 'rgba(34,34,34,0)' }));
}
handleClick(e) {
e.preventDefault();
const elementOffsetTop = document.getElementById(e.target.innerText).offsetTop;
window.scrollTo(0, elementOffsetTop);
}
handleUp(e) {
e.preventDefault();
window.scrollTo(0, 0);
}
render() {
const menuItems = [
{ menuItem: 'O nas', link: 'About' },
{ menuItem: 'Księgowość', link: 'Ksiegowosc' },
{ menuItem: 'Kadre i płace', link: 'Kadre' },
{ menuItem: 'Doradztwo', link: 'Doradztwo' },
{ menuItem: 'Nieruchomości', link: 'nieruchomosci' },
{ menuItem: 'Kontakt', link: 'kontakt' }
];
const items = menuItems.map(item => {
const styles = {
linkStyle: {
textDecoration: 'none',
color: '#ffffff',
cursor: 'pointer'
},
textStyle: {
marginLeft: '1rem',
textTransform: 'uppercase'
}
};
const { linkStyle, textStyle } = styles;
return (
<a onClick={e => this.handleClick(e)} key={item.link} style={linkStyle}>
<p style={textStyle}> {item.menuItem} </p>
</a>
);
});
const styles = {
containerStyle: {
height: 70,
position: 'fixed',
top: 0,
width: '100%',
backgroundColor: this.state.backgroundColor,
zIndex: 20000,
},
headerStyle: {
height: 70,
},
navigationStyle: {
height: '100%',
color: 'white',
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
fontSize: '0.9rem'
},
navigationItemsStyle: {
display: 'flex',
},
logoStyle: {
fontSize: '1.3rem',
cursor: 'pointer'
}
};
const { headerStyle,
containerStyle,
navigationStyle,
navigationItemsStyle,
logoStyle
} = styles;
return (
<div id="header" style={containerStyle}>
<header style={headerStyle} ref='header'>
<Container>
<div style={navigationStyle}>
<a onClick={e => this.handleUp(e)} style={logoStyle}>
<div>{this.props.text}</div>
</a>
<div style={navigationItemsStyle}> {items} </div>
</div>
</Container>
</header>
</div>
);
}
}
export default Header;
you can use react-intersection-observer to achieve that.
More info about that on react-intersection-observer

Categories

Resources