šŸŽ Checkout my Learn React by Making a Game course and get 1 month Free on Skillshare!

How to build a React Edit Cancel Text Input component

In a recent project, at one point we needed to build an cancel editable field. Let me show you what I mean. For example if you double click this word it will become a text input and I can set a new value for it. On the other side edit and later decide to cancel the text will remain the same. What you see in here is a jQuery plugin, so we will need a way to replicate this in react.

These examples are created by using create-react-app. You can check here a short guide on how to setup your machine to run the code samples.

If you want you can take this as an exercise and try first to do it by your own, and later come back to check the solution.

Going to back to our stater files, what we have in this initial setup is just a basic component that shows this value from the state.

First thing will be to make this text to react to double click in order to show the edit view. This will be quite a easy win as I will just have to declare here the onDoubleClick event, bind it a function that I will call changeEditMode, and will also create this changeEditMode with just a simple console log message. After the save, if we click on the text, will get this message.

Now, the state needs to remember somehow if it's in edit mode or not. For this I will add a new variable on to the state called isInEditMode, and by default a component is not in edit mode, so this will be false.

Having this set , I can come now in the render and check is this.state.isInEditMode, and if so , will return for now just div with a empty text input that has it's default value set to this.state.value. Otherwize, will return the old stuff.

However this will not be enough, as we will also need to change this isInEditMode state variable. So when somebody does a double click, the change edit mode is called, so here, instead of the console log I will say this dot set state and isInEditMode becomes not this.state.isInEditMode.

As this will part here will become a bit more complex let's extract these two views to their own methods. Will create the renderEditView that will replace this JSX part and return it ... and also the renderDefaultView that will replace this and return it.

Now that we have a cleaner code, let's find a way to show back the default view after we go into edit mode. For this will create here a new button will have X as value and will call on click [occ] the this.changeEditMode to change toggle back the edit mode. ... and it seems to work.

And the last peace of the puzzle. Will have to update this value if the user clicks ok. So first we will need this ok button, and will add of it a onClickHandler that will call a method updateComponentValue. Let's also create this method. Inside it we will need do some state updates. First to close the edit view, by setting isInEditMode to false, but also to update this value.

To update the value, have to read the new one from here. For this particular case, is a better alternative to have refs for this input. So I will go and add here a ref of theTextInput and with this we can just go and int the updateComponentValue use this.refs the text input and value.

And this should do the trick. If I double click now, add another text and save, all seems ok, and I try to edit and cancel ā€¦ then all comes back to the previous stage.

So there you have it. A nice cancel - editabile component made with React. Maybe is not the most advanced component, but I think si quite nice as a beginner example. Cheers and see you the next time.

šŸ“– 50 Javascript, React and NextJs Projects

Learn by doing with this FREE ebook! Not sure what to build? Dive in with 50 projects with project briefs and wireframes! Choose from 8 project categories and get started right away.

šŸ“– 50 Javascript, React and NextJs Projects

Learn by doing with this FREE ebook! Not sure what to build? Dive in with 50 projects with project briefs and wireframes! Choose from 8 project categories and get started right away.


2 Replies

  • Great tutorial! I was wondering if it worked on mobile? for example if people tap on their phone or double tap does it work like a double click?

  • Hi Ari,
    Thanks for your comment. It should work at least for Android. In the past I know there were some issues with iOS on double tap events. Not sure what is the current status these days.

Leave a Reply

Your email address will not be published. Required fields are marked *