r/reactjs • u/timmonsjg • Feb 01 '19
Needs Help Beginner's Thread / Easy Questions (February 2019)
π This month we celebrate the official release of Hooks! π
New month, new thread π - January 2019 and December 2018 here.
Got questions about React or anything else in its ecosystem? Stuck making progress on your app? Ask away! Weβre a friendly bunch.
No question is too simple. π€
Last month this thread reached over 500 comments! Thank you all for contributing questions and answers! Keep em coming.
π Want Help with your Code? π
- Improve your chances by putting a minimal example to either JSFiddle or Code Sandbox. Describe what you want it to do, and things you've tried. Don't just post big blocks of code!
- Pay it forward! Answer questions even if there is already an answer - multiple perspectives can be very helpful to beginners. Also there's no quicker way to learn than being wrong on the Internet.
Have a question regarding code / repository organization?
It's most likely answered within this tweet.
New to React?
π Here are great, free resources! π
- Create React App
- Read the official Getting Started page on the docs.
- /u/acemarke's suggested resources for learning React
- Kent Dodd's Egghead.io course
- Tyler McGinnis' 2018 Guide
- Codecademy's React courses
- Scrimba's React Course
- Robin Wieruch's Road to React
Any ideas/suggestions to improve this thread - feel free to comment here or ping /u/timmonsjg :)
View all comments
1
u/snsarma May 09 '19
Hi Folks
I have been trying to implement the feature of user inactivity on my react app ,
That is if a logged in user is inactive for a certain period of time ,
he/she should get a notification that they are going to be logged out in a minute or so and then eventually be logged out .
I am doing this by detecting user's mouse event or keyboard events and touch screen events , if none of them occur they should be logged out.
That isn't happening in my code :
function myFunction(details) {
console.log('In myFunction ');
var x = document.getElementById("search");
var y = document.getElementById("details");
if (details === true) {
x.style.display = "none";
y.style.display = "block";
} else {
y.style.display = "none";
x.style.display = "block";
}
}
class blank {
componentDidMount() {
console.log('In component did Mount of blank');
myFunction(false);
this.init();
//myFunction(false);
}
init = () => {
console.log('In init');
/*events = {
'load':false,
'mousemove':false,
'mousedown':false,
'click':false,
'scroll':false,
'keypress':false,
};*/
events[0] = {"load":false};
events[1] = {"mousemove":false};
events[2] = {"mousedown":false};
events[3] = {"click":false};
events[4] = {"scroll":false};
events[5] = {"keypress":false};
console.log('events[0]',events[0]);
var i =0;
for (;events.length;++i ) {
//window.addEventListener(this.events[i], this.resetTimeout);
window.addEventListener(events[i], function (event) {
// If the clicked element doesn't have the right selector, bail
if (!(event.target.matches(events[i]))) return;
// Don't follow the link
event.preventDefault();
// Log the clicked element in the console
console.log('event.target',event.target);
},false);
console.log('In component did mount of blank with this.events[i]',this.events[i]);
// ++i;
}
this.setTimeout();
};
clearTimeoutFunc = () => {
console.log('In clearTimeoutFunc of blank');
if (this.warnTimeout) clearTimeout(this.warnTimeout);
if (this.logoutTimeout) clearTimeout(this.logoutTimeout);
};
setTimeout = () => {
console.log('In setTimeout of blank');
console.log('load',"load");
if("load" === false ||'mousemove' === false || 'mousedown' === false || 'click' === false || 'scroll' === false || 'keypress' === false || 'ontouchstart' === false ){
this.warnTimeout = setTimeout(this.warn(), this.state.warningTime);
this.logoutTimeout = setTimeout(this.logout(), this.state.signoutTime);
}
//this.warn();
console.log('In setTimeout of blank warnTimeout',this.warnTimeout);
console.log('In setTimeout of blank logoutTimeout',this.logoutTimeout);
};
resetTimeout = () => {
console.log('In resetTimeout of blank');
this.clearTimeoutFunc();
this.setTimeout();
};
warn = () => {
window.alert("You will be logged out automatically in 1 minute")
/*swal({
title: "You will be logged out automatically in 1 minute!",
icon: "warning",
confirmButtonText: 'OK'
}).then((okay) => {
if (okay) {
history.push('/grid');
history.push('/blank');
}
});*/
console.log('You will be logged out automatically in 1 minute.');
};
logout = () => {
// Send a logout request to the API
console.log('Sending a logout request to the API...');
this.destroy();
};
destroy = () => {
//clear the session
//browserHistory.push('/');
//window.location.assign('/');
console.log('In destroy function');
history.push('/'); window.location='/';sessionStorage.setItem('login','');
};
}
export default blank;