2017-09-11 18:11:45 -07:00
# Custom Nodes
2017-09-07 14:33:34 -07:00
2017-09-11 18:11:45 -07:00
Slate will render custom nodes for [`Block` ](../slate/block.md ) and [`Inline` ](../slate/inline.md ) models, based on what you pass in as your schema. This allows you to completely customize the rendering behavior of your Slate editor.
2017-09-07 14:33:34 -07:00
2017-10-27 13:39:06 -07:00
## Props
2017-09-07 14:33:34 -07:00
```js
< {Custom}
attributes={Object}
children={Object}
editor={Editor}
isSelected={Boolean}
node={Node}
parent={Node}
readOnly={Boolean}
/>
```
### `attributes`
2018-02-07 15:58:41 +00:00
2017-09-07 14:33:34 -07:00
`Object`
A dictionary of DOM attributes that you must attach to the main DOM element of the node you render. For example:
```js
2018-02-07 15:58:41 +00:00
return < p { . . . props . attributes } > {props.children}< / p >
2017-09-07 14:33:34 -07:00
```
2018-02-07 15:58:41 +00:00
2017-09-07 14:33:34 -07:00
```js
return (
< figure { . . . props . attributes } >
< img src = {...} / >
< / figure >
)
```
### `children`
2018-02-07 15:58:41 +00:00
2017-09-07 14:33:34 -07:00
`Object`
A set of React children elements that are composed of internal Slate components that handle all of the editing logic of the editor for you. You must render these as the children of your non-void nodes. For example:
```js
2018-02-07 15:58:41 +00:00
return < p { . . . props . attributes } > {props.children}< / p >
2017-09-07 14:33:34 -07:00
```
### `editor`
2018-02-07 15:58:41 +00:00
2017-09-07 14:33:34 -07:00
`Editor`
2017-10-27 13:39:06 -07:00
A reference to the Slate [`<Editor>` ](./editor.md ) instance. This allows you to retrieve the current `value` of the editor, or perform a `change` on the value. For example:
2017-09-07 14:33:34 -07:00
```js
2017-10-27 13:39:06 -07:00
const value = editor.value
2017-09-07 14:33:34 -07:00
```
2018-02-07 15:58:41 +00:00
2017-09-07 14:33:34 -07:00
```js
2018-02-07 15:58:41 +00:00
editor.change(change => {
2017-09-07 14:33:34 -07:00
change.selectAll().delete()
})
```
### `isSelected`
2018-02-07 15:58:41 +00:00
2017-09-07 14:33:34 -07:00
`Boolean`
A boolean representing whether the node you are rendering is currently selected. You can use this to render a visual representation of the selection.
### `node`
2018-02-07 15:58:41 +00:00
2017-09-07 14:33:34 -07:00
`Node`
2017-09-11 18:11:45 -07:00
A reference to the [`Node` ](../slate/node.md ) being rendered.
2017-09-07 14:33:34 -07:00
### `parent`
2018-02-07 15:58:41 +00:00
2017-09-07 14:33:34 -07:00
`Node`
2017-09-11 18:11:45 -07:00
A reference to the parent of the current [`Node` ](../slate/node.md ) being rendered.
2017-09-07 14:33:34 -07:00
### `readOnly`
2018-02-07 15:58:41 +00:00
2017-09-07 14:33:34 -07:00
`Boolean`
Whether the editor is in "read-only" mode, where all of the rendering is the same, but the user is prevented from editing the editor's content.
2017-09-19 17:52:38 +02:00
## `shouldNodeComponentUpdate`
By default, Slate implements a `shouldComponentUpdate` preventing useless re-renders for node components. While the default implementation covers most use cases, you can customize the logic to fit your needs. For example:
```js
class CustomNode extends React.Component {
static shouldNodeComponentUpdate(previousProps, nextProps) {
// return true here to trigger a re-render
}
}
```
If `shouldNodeComponentUpdate` returns false, Slate will still figure out whether a re-render is needed or not.