How to display an error message for a specific time period within ReactJS?

KenB

I am using ReactJS. In my app, if an error occurs, I need to display a message to the user for a programable amount of time.

I would normally implement a stateless UI component based only on the props, but knowing I need a timer, I instead create a React Component called Message to manage the timer and state.

export class Message extends React.Component {...}

Within this Message component, I convert the props to state within the constructor, and update the state within componentWillReceiveProps if nextProps are different from the present state. Also within the component, I start the timer if there is a timeout specified. At timeout, the components state of the message is nulled, which results in the component no longer displaying the message.

When the App receives an error, it creates the message:

<Message message="You have big problems." timeout=5000 />

and all works as expected, the error message is displayed for 5 seconds.


But I have been unable to get it working properly to simultaneously manage these two use cases:

Case 1: After the first error occurs and has been displayed, a second error can occur that is identical to the first, requiring the error to be displayed again.

Case 2: The App can re-render based on other state changes, in which case we are not to re-display the error.

To manage case 1, I set the state of message within the Message component to null after the timer expires. Since this message state variable is used as the condition to display the message, the message disappears. When a second error occurs with a message equivalent to the first error, the new message prop is now different from the nulled message state within the Message component, causing a new message to be displayed for a new timeout.

This works great except now when case 2 occurs, it causing a re-render of the components, including a re-render of the last error message that occurred, inappropriately resulting in the user seeing another message displayed.

I know that if I lifted up the timer out of the Message component and placed it in the app/parent, I could cause the Message component to be rendered after the timeout, and this would make it work, but it seems so wrong to not be able to manage the timeout lifecycle in the Message component itself.

Is there any solution to this? Or did I indeed discover the scenario that cannot be easily managed with ReactJS?

Thank you.

Evan Trimboli

One way to control it is to have a couple of extra props on your Message:

onTimeout() {
  this.setState({ open: false });
}

render() {
  const { open } = this.state;
  <Message open={open} onTimeout={this.onTimeout} message="..." timeout={5000} />
}

The open prop would control whether the message is rendered at all. If open is set to false, any timers should be cancelled. The onTimeout function should be called once the timeout fires, from here, you can control the visibility state.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to find how many entries are within a specific time period

How to grep a log file within a specific time period

How to remove a message after a specific period of time in DiscordJS?

How to display an error message within the form

Answer to message if sent within a period of time

Calculate number of occurrences within a specific time period

Proceed if current time is within the 15 minute period after a specific time

How to return and display an error message from within a exception try catch

How to find display error message from API in reactjs

How to display an Error message when logging in fails in Reactjs

How Can I Display a Message Box That Has No Buttons, and That Disappears After a Specified Period of Time?

JavaScript display array elements within certain time period

Count changes from specific value within time period

How to display a DIV during a period of time? Javascript

How to display an activity after certain time period

How to display a label for a short period of time in kivy?

Counting within a period of time

How delete rows that are not in a specific time period?

How to schedule tcpdump to run for a specific period of time?

How to convert data in a specific period of time to nan?

count how many specific days are in a time period

how to grep for specific time period in a log

How to get the next status for a specific period of time?

How to remove first rows in a specific period of time?

how to disable login after specific period of time

How to display the error message

How to do I exclude a specific date range within a specific period?

How to find the nearest time period within a vector of time objects?

ReactJS Formik display error message in Red color