mobx-preact-devtools
v0.0.19
Published
Dev-tools for MobX and Preact
Downloads
5
Maintainers
Readme
mobx-preact-devtools (in developing)
DevTools for MobX to track the rendering behavior and data dependencies of your app.
It is a fork from original react mobx devtools (https://github.com/mobxjs/mobx-react-devtools) to work with preact instead of react.
Installation
npm install --save-dev mobx-preact-devtools
Usage
Somewhere in your application, create a DevTools component:
import { h, Component } from 'preact';
import DevTools from 'mobx-preact-devtools';
class MyApp extends Component {
render() {
return (
<div>
...
<DevTools />
</div>
);
}
}
Supported props:
highlightTimeout
— number, default: 1500noPanel
— boolean, if set, do not render control panelposition
— object, position of control panel, default:{ top: -2, right: 20 }
className
— string, className of control panelstyle
— object, inline style object of control panelposition
— string or object,topRight
,bottomRight
,bottomLeft
ortopLeft
, default:bottomRight
The position of the panel can be tweaked by setting the value to an object with top
, right
, bottom
or left
defined. Setting it to { top: -2, right: 20 }
is the same as topRight
.
From there on, after each rendering a reactive components logs the following three metrics:
- Number of times the component did render so far
- The time spend in the
render()
method of a component - The time spend from the start of the
render()
method until the changes are flushed to the DOM
For each component the color indicates roughly how long the coloring took. Rendering times are cumulative; they include time spend in the children
- Green: less then 25 ms
- Orange: less then 100 ms
- Red: rendering for this component took more than 100ms
About log groups
Note that if logging is enabled, MobX actions and reactions will appear as collapsible groups inside the browsers console. Mind that any log statements that are printed during these (re)actions will appear inside those groups as well, so that you can exactly trace when they are triggered.
Configuration
import { configureDevtool } from 'mobx-preact-devtools';
// Any configurations are optional
configureDevtool({
// Turn on logging changes button programmatically:
logEnabled: true,
// Turn off displaying components updates button programmatically:
updatesEnabled: false,
// Log only changes of type `reaction`
// (only affects top-level messages in console, not inside groups)
logFilter: change => change.type === 'reaction',
});
There are also aliases for turning on/off devtools buttons:
import { setLogEnabled, setUpdatesEnabled, setGraphEnabled } from 'mobx-preact-devtools';
setLogEnabled(true); // same as configureDevtool({ logEnabled: true });
setUpdatesEnabled(false); // same as configureDevtool({ updatesEnabled: false });
setGraphEnabled(false); // same as configureDevtool({ graphEnabled: false });
Custom panel design
import { h, Component } from 'preact';
import DevTools, { GraphControl, LogControl, UpdatesControl } from 'mobx-preact-devtools';
class MyNiceButton extends Component {
render() {
const { active, onToggle, children } = this.props;
return (
<button onClick={onToggle}>
{children}
{active ? ' on' : ' off'}
</button>
);
}
}
class MyApp extends Component {
render() {
return (
<div>
{/* Include somewhere with `noPanel` prop. Is needed to display updates and modals */}
<DevTools noPanel />
<div className="my-custom-devtools-panel-design">
<GraphControl>
{/* Must have only one child that takes props: `active` (bool), `onToggle` (func) */}
<MyNiceButton>Graph</MyNiceButton>
</GraphControl>
<LogControl>
{/* Must have only one child that takes props: `active` (bool), `onToggle` (func) */}
<MyNiceButton>Log</MyNiceButton>
</LogControl>
<UpdatesControl>
{/* Must have only one child that takes props: `active` (bool), `onToggle` (func) */}
<MyNiceButton>Updates</MyNiceButton>
</UpdatesControl>
</div>
</div>
);
}
}
Changelog
0.0.1 Changed Readme, package.json and remote git repo
0.0.2 Migrated to preact from react, added babel plugin and preset for preact
0.0.3 updated Readme
0.0.5 removed prop-types, because they are compatible with preact
0.0.6 removed all mobx-react dev dependencies
0.0.7 removed React in UpdatedControl.jsx
0.0.8 removed cloneElement method from react library
0.0.9 moved PanelButton to main components: LogControl, GraphControl and UpdatesControl