Build a custom JSON editor extension for Replit using React and react-json-view to enable structured editing and code folding of JSON files.
react-json-view
package with npm install --force react-json-view
. The package uses React 17 as a peer dependency but works fine with React 18 as well.public/extension.json
(the Extension manifest file).
fileHandlers
property to extension.json
and provide a handler for JSON files. This tells Replit that your extension handles a particular file pattern using a page provided by your extension at the handler
path. In this case, the handler is /
, meaning that Replit shows the page at the root as the handler for all .json
files
src/App.jsx
.
App
function and a state variable path
, which will point to the JSON file your Extension will render.
status
and error
properties from the useReplit hook
within the App
function.
The status
property is an enumerated value indicating whether the handshake connection with Replit is loading
, ready
, or has resulted in an error
.
useReplitEffect
hook and set the path
state to the extensionPort
’s file path. This will set the path
state once the handshake between Replit and your Extension has been established.
useWatchTextFile
hook. Call the hook, pass in the file path, and derive content
and writeChange
from it.
parsedContent
React Memo that returns the content
file value as parsed JSON. If there is an error parsing it, return null
instead.
The React useMemo hook caches a result based on an array of dependencies between re-renders to improve performance. Caching reduces the amount of computing required in a process, ultimately improving performance.
updated_src
property passed into this function is a JSON object.
Stringify the JSON object and then write it to the JSON file using the writeChange
function.
Finally, update the setContent
state to reflect the contents of the file.
path
and content
are valid strings, render the editor. If not, tell the user to select a file.
App.css
: