Editmode Docs
Search…
Editmode for React
Editmode allows you to turn plain text in your React app into easily inline-editable bits of content that can be managed by anyone with no technical knowledge.

Installation

NPM
Yarn
1
npm install editmode-react
Copied!
1
yarn add editmode-react
Copied!

Usage

Step 1:

Within your React app, navigate to the index file within your src directory. Import the Editmode wrapper and wrap your App within.
Basic Setup
Specifying a branch
Specifying default content
1
import { Editmode } from "editmode-react";
2
3
// 👉 `project_id` can be found in the URL:
4
// https://editmode.com/projects/{project_id}/chunks
5
6
ReactDOM.render(
7
<React>
8
<Editmode projectId={project_id}>
9
<App />
10
</Editmode>
11
</React>,
12
document.getElementById("root")
13
);
Copied!
1
import { Editmode } from "editmode-react";
2
3
// Branches are used to create separate "versions" of your content, which can
4
// be useful for staging, running a/b tests, or developing locally on teams.
5
// Branches are created within the content hub at https://app.editmode.com
6
// Specifying a branch id when initializing Editmode will load all content
7
// from that specific branch.
8
9
ReactDOM.render(
10
<React>
11
<Editmode projectId={project_id} branchId={branch_id}>
12
<App />
13
</Editmode>
14
</React>,
15
document.getElementById("root")
16
);
Copied!
1
import { Editmode } from "editmode-react";
2
import { defaultChunks } from "./data/defaultChunks";
3
4
// Editmode is smart about fetching and caching content to ensure that 1. Your
5
// app/site always remains fast, and 2. Users always see the most recent content.
6
// While Editmode is capable of loading content direct from our CDN, some
7
// customers like to also bundle a resource file within their codebase that will
8
// provide content in the event that the API fails. This can
9
// be achieved by passing an object with default content to the Editmode provider
10
// on initialization. The creation of this file can be automated via our API.
11
// See https://snack.expo.io/@editmode/algolia-demo for an interactive demo
12
// of this setup
13
14
ReactDOM.render(
15
<React>
16
<Editmode projectId={project_id} defaultChunks={defaultChunks} >
17
<App />
18
</Editmode>
19
</React>,
20
document.getElementById("root")
21
);
Copied!

Step 2:

Rendering a chunk:

If you have already created the chunk you would like to render on the Editmode CMS, you can simply pass the identifier as a prop and begin editing. You can provide default content as a fallback should anything go wrong trying to retrieve the data from the API:
Render an editable chunk
Render a hybrid chunk field
Specify classes
1
import { Chunk } from "editmode-react";
2
3
function Example() {
4
return (
5
<section>
6
7
{/* Reference a standalone chunk using the chunk identifier */}
8
<Chunk identifier="cnk_7019e843b76e2d0395ab" />
9
10
{/* You can also reference a chunk using its content key */}
11
<Chunk identifier="company_name" />
12
13
{/* Provide default content when referencing a chunk */}
14
{/* Default content is a precaution that will get rendered in
15
the event that the content cannot be served from the Editmode API.
16
*/}
17
<Chunk identifier="company_name">
18
Our Company
19
</Chunk>
20
21
</section>
22
);
23
}
Copied!
1
import { Chunk } from "editmode-react";
2
3
4
function Example() {
5
return (
6
<section>
7
8
{/* Editmode has two types of chunks - "standalone" and "hybrid".
9
Standalone chunks can only store a single piece of content,
10
whereas hybrid (or collection) chunks can have many fields.
11
These fields are pre-specified in the Content Hub at
12
https://app.editmode.com
13
*/}
14
15
<Chunk identifier="home_hero" field="Headline" />
16
<Chunk identifier="home_hero" field="Tagline" />
17
<Chunk identifier="home_hero" field="Description" />
18
19
</section>
20
);
21
}
Copied!
1
import { Chunk } from "editmode-react";
2
3
// By default, an editable chunk is rendered to the client as an unstyled <em-span />
4
// This can cause unwanted behaviour from a styling perspective, so you can tell
5
// Editmode to add a class to the wrapper.
6
7
function Example() {
8
return (
9
<section>
10
<Chunk identifier="company_name" className="bg-white rounded shadow p-6" />
11
</section>
12
);
13
}
Copied!

Rendering a chunk collection:

Chunk collections are simply a way to group chunks and can be used to render repeatable content. Each collection can contain many properties and each property can hold different types of information.
A good use case example would be creating a "Team Member" collection. It may have Full Name, Title and Headshot properties. Within your React app, you may want to display the name, title and headshot of all your team members (i.e. all chunks within the Team Member collection). You can do this by passing the chunk collection identifier as a prop to the ChunkCollection component. For example...
Basic collections w/ inline editing
Going Deeper
1
import { ChunkCollection, ChunkFieldValue } from "editmode-react";
2
3
function Example() {
4
return (
5
<section className="testimonials">
6
7
{/* Render content from a collection, with inline editing */}
8
<ChunkCollection identifier="col_MFxBu6fiTyRM" >
9
<ChunkFieldValue identifier="fld_LscoanYMdCOy" />
10
<ChunkFieldValue identifier="fld_Iq94B0LyQxGc" />
11
<ChunkFieldValue identifier="fld_LyRI6y3v2D8ct" />
12
</ChunkCollection>
13
14
{/* Use content keys for better readability */}
15
<ChunkCollection identifier="testimonials" >
16
<ChunkFieldValue identifier="Name" />
17
<ChunkFieldValue identifier="Role" />
18
<ChunkFieldValue identifier="Comment" />
19
</ChunkCollection>
20
21
{/* Only render collection items with certain tags */}
22
<ChunkCollection identifier="testimonials" tags={["home_testimonials"]}>
23
<ChunkFieldValue identifier="Name" />
24
<ChunkFieldValue identifier="Role" />
25
<ChunkFieldValue identifier="Comment" />
26
</ChunkCollection>
27
28
</section>
29
);
30
}
Copied!
1
import { ChunkCollection, ChunkFieldValue, getChunk } from "editmode-react";
2
3
// Often, when iterating through a collection of content, your UI will need
4
// to access the raw values, instead of rendering inline-editable chunks.
5
// For this we use getChunk(), along with ChunkCollection.
6
7
function Example() {
8
return (
9
<section className="meet_the_team">
10
<ChunkCollection identifier="navigation_items">
11
{(getChunk, chunk) => {
12
return (
13
if (getChunk(chunk, "Title")) {
14
15
{/* Render an editable inline field */}
16
<ChunkFieldValue identifier="Title" />
17
18
{/* Render a link using the chunk field values*/}
19
<a href={getChunk(chunk, "Url")}>
20
{getChunk(chunk, "Title")}
21
</a>
22
}
23
)
24
}}
25
</ChunkCollection>
26
</section>
27
);
28
}
Copied!
ChunkCollection Attributes
Attribute
Type
Description
identifier
string
Takes the id of a collection you want to loop through
limit
int string
optional The number of collection items you want to display
tags
array
optional Filter collection items based on tags listed in this prop
className
string
optional Class name(s) that will be added along with “chunks-collection-wrapper” to the main collection <div> element
itemClass
string
optional Class name(s) that will be added along with “chunks-collection-item–wrapper” to all collection items

ChunkFieldValue Attributes

Attribute
Type
Description
identifier
string
Takes the identifier or field_name of a collection field
className
string
optional Class name(s) that will be added in the chunk em-span element

Variables

Variables that are created in the Editmode CMS are also supported by passing an object prop as variables.
1
function Example() {
2
return (
3
<section>
4
<Editmode projectId="prj_h3Gk3gFVMXbl">
5
<Chunk identifier="welcome_message" variables={{ "name": "John" }} />
6
</Editmode>
7
</section>
8
);
9
}
Copied!
With this, chunks such as Hello, {{name}}! will be parsed as Hello, John!

Image Transformations

Editmode is capable of performing all kinds of transformations on your images before they're rendered within your web app or web site
The transformation attribute is used to perform real-time image transformations to deliver perfect images to the end-users.
1
// This chunk should render an image with 200 x 200 dimension
2
<Chunk identifier='cnk_23123123' transformation="w-200 h-200" />
3
4
// For image inside a collection
5
<ChunkCollection identifier="col_123...">
6
<ChunkFieldValue identifier='Avatar' transformation="w-200 h-200" />
7
</ChunkCollection>
Copied!

Using the Magic Editor

You can now edit and save all of the chunks in your React app from within the browser - just add editmode=1 as a query string parameter to the current URL to initialize the Magic Editor.
Last modified 4mo ago