diff --git a/README.md b/README.md index ab9d626..8807e9f 100644 --- a/README.md +++ b/README.md @@ -96,13 +96,17 @@ export interface Props { ## What's happening here? -For simple applications with just a few components, this is a quick pattern to embed serialized information into your HTML. The `Serialize` component will do this for you, currently by using a hidden `textarea` element to hold the string. +This is a quick and easy pattern to embed serialized information into your HTML and make it available in the client-side script. -The `deserialize()` function can then parse the value string for use in your client script. You have to manage the IDs yourself (i.e., make sure they are unique) and understand that the `deserialize()` function will crawl the whole document incurring some performance cost. +The `Serialize` component will write the data as JSON wrapped in a ` \ No newline at end of file diff --git a/src/deserialize.ts b/src/deserialize.ts index 351721d..74154b8 100644 --- a/src/deserialize.ts +++ b/src/deserialize.ts @@ -1,8 +1,8 @@ export function deserialize(id: string): T { - const element = document.querySelector(`#${id}`); + const element = document.querySelector(`#${id}`); - if (element?.value) - return JSON.parse(element.value) + if (element?.innerText) + return JSON.parse(element.innerText) throw Error(`The call deserialize('${id}') did not find any data. Check that the following are correct: