Cookie Consent by Free Privacy Policy Generator Update cookies preferences 📌 Content editable element in React.

🏠 Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeiträge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden Überblick über die wichtigsten Aspekte der IT-Sicherheit in einer sich ständig verändernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch übersetzen, erst Englisch auswählen dann wieder Deutsch!

Google Android Playstore Download Button für Team IT Security



📚 Content editable element in React.


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

In recent time, I was tasked with implementing a feature that allows users to edit content seamlessly, such as modifying paragraphs or headings. I explored various approaches to tackle this challenge, ranging from toggling text elements to input elements upon clicking or onBlur events, to leveraging the 'editable' prop in the Ant Design library.

All the above suggestions worked but not giving me the result I was looking for. Then I learnt something new. I figured you can also make text elements (paragraph tags, header tags etc.) editable seamlessly with an inbuilt attribute called contentEditable.

So contentEditable prop when passed, would make the element editable by just clicking on it, it can be a lot helpful when building rich editors so basically what this is doing is giving user the ability to inject html directly on the DOM(Document Object Model) which if we are being honest can be risky due to browsers inconsistency so it should be handled properly.

How it works

contentEditable is a property In the html element that help with rendering rich texts in the browser, and editing it on the fly, making an element editable is quite straight forward.

Let’s see an example:

Example of how to make an editable content

From the example above we noticed the props being passed into the element

contentEdtable: This prop is set to true, which signifies to the browser that this element is editable

suppressEditableContentWarning: This prop is also added to help remove warning that’s arise when using the contentEditable prop, this is a warning you can possibly get “A component is contentEditable and contains children managed by react” if using it in a react project.

Now, let’s save the value that is being typed in our editable element.

Example of how to save the editable data

Now in this example, we can clearly see we how we catch values typed into our editable content and save it in a state. And if we go over to our console and check after the onBlur function is fired you should she your value rendered. In our case "Hello there"

result of out console log

There are other methods you can use to save your data, I decided to use onBlur by discretion, but you can choose to use other functions like onInput, onMouseOut, onKeydown etc.

NB. Editable elements do not have onChange function like form elements(input tags, textarea tags etc).

Let's do one last trick. We now have our editable content up and working, but just like inputs have placeholders when it is empty, we too can add placeholders to our elements when empty.

Here is how:

html image for showing placeholder

In the image above you can see we added two new attributes

data-placeholder: This attribute helps us add the appropriate placeholder to our element. This is where we would add our placeholder value

className: This is us adding a className to our editable element so we can target it in our css.

Then we our final strike as we would have known is going to be in our css file.

Image of the css file

With this css properties added to our element class our element should have the placeholder specified by the data-placeholder in the html.

And that's it guys!

We have successfully created an editable element, saved it's state locally and also added placeholder when it is empty.

Best wishes on your development journey.

...



📌 Content editable element in React.


📈 49.75 Punkte

📌 CVE-2022-36010 | oxyno-zeta react-editable-json-tree prior 2.2.2 neutralization of directives (GHSA-j3rv-w43q-f9x2)


📈 30.97 Punkte

📌 This Week In React #185: React Conf, React Query, refs, Next.js after, mini-react...


📈 30.62 Punkte

📌 This Week In React #185: React Conf, React Query, refs, Next.js after, mini-react...


📈 30.62 Punkte

📌 Mozilla Firefox bis 46 DOM Element Handler mozilla::dom::Element Pufferüberlauf


📈 23.68 Punkte

📌 Element N.v Element Instantshop 1.0 add_2_basket.asp price privilege escalation


📈 23.68 Punkte

📌 Microsoft Internet Explorer 9/10 SVG Element Text Element memory corruption


📈 23.68 Punkte

📌 CVE-2022-22512 | VARTA Element Backup/Element S Web UI hard-coded credentials (VDE-2022-061)


📈 23.68 Punkte

📌 Mozilla Firefox bis 46 DOM Element Handler mozilla::dom::Element Pufferüberlauf


📈 23.68 Punkte

📌 how i can click a child element inside of element ?


📈 23.68 Punkte

📌 Tory party's conference app editable by world+dog, blabs party members' digits


📈 23.32 Punkte

📌 Using Strapi Policies To Create Editable User Profiles


📈 23.32 Punkte

📌 Friendly Data Modeling & Auto-generated, Editable Migrations for Platformatic with Prisma


📈 23.32 Punkte

📌 How to make a PDF non-editable on Mac


📈 23.32 Punkte

📌 #87 Editable Shortcuts · This Week in GNOME


📈 23.32 Punkte

📌 Azure flaw left Bing results editable and MS 365 accounts exposed


📈 23.32 Punkte

📌 EXE Editable with Resource Hacker 2


📈 23.32 Punkte

📌 Microsoft Excel Can Now Turn Pictures of Tables Into Actual, Editable Tables


📈 23.32 Punkte

📌 curl: Github wikis are editable by anyone #Githubwikistakeover


📈 23.32 Punkte

📌 Paragon Initiative Enterprises: Github wikis are editable by anyone https://github.com/paragonie/password_lock/wiki


📈 23.32 Punkte

📌 XSS via editable slug fields


📈 23.32 Punkte

📌 What is the best way to ensure that a PDF file is absolutely un-editable?


📈 23.32 Punkte

📌 Is there a free (for personal use) virtual machine with an editable .vmx file for M1 mac


📈 23.32 Punkte

📌 ChatGPT unveils Google Drive & OneDrive integration and real-time editable tables 


📈 23.32 Punkte

📌 Meet GeoCode: An Artificial Intelligence Technique For 3D Shape Synthesis Using An Intuitively Editable Parameter Space


📈 23.32 Punkte

📌 How to Convert Screenshots to Editable Figma Designs with Codia AI in 3 Easy Steps


📈 23.32 Punkte

📌 DALL·E Images Now Editable Directly in ChatGPT on Web and Mobile Platforms


📈 23.32 Punkte

📌 SC-GS: Sparse-Controlled Gaussian Splatting for Editable Dynamic Scenes


📈 23.32 Punkte

📌 Snapi: An editable gallery for photographers


📈 23.32 Punkte

📌 This Week In React #127: Nextra, React-Query, React Documentary, Storybook, Remix, Tamagui, Solito, TC39, Rome...


📈 22.96 Punkte

📌 This Week In React #131: useReducer, Controlled Inputs, Async React, DevTools, React-Query, Storybook, Remix, RN , Expo...


📈 22.96 Punkte

📌 This Week In React #139: React.dev, Remix, Server Components, Error Boundary, Wakuwork, React-Native, Bottom Sheet...


📈 22.96 Punkte











matomo