百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

react-demo

> 编程语言
开源

用于创建其他组件演示的 React 组件

100 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

用于创建其他组件演示的 React 组件

React-demo

A React-component for creating demos of other components. See also react-demo-library

Example

javascript
import React from 'react'
import Demo, {props as P} from 'react-demo'
import MyButton from './my-components/MyButton'

React.render(, el)

See examples/index.js for more examples. To run them locally do the following:

$ npm install
$ npm run examples
$ open http://localhost:3000

Installation

$ npm install react-demo

API

The Demo component

Demo is the main component of this library. It allows us to create a demo of our components with controllable props.

javascript
import Demo from 'react-demo'

target

The target component that we want to create a demo of.

fullWidth

If set to true full page width will be available to the demo target component. More precisely full width that avaliable to the Demo component.

background

Sets the background of the demo. Available options are "light", "dark", and "none". The default value is "light" which gives us a Photoshop style light "transparent" background.

codeIndentDepth

Specifies indentation of component code that is shown in control panel. The more the value the deeper indentation will go.

props

This property allows us to configure the props for the demo target. Accepts an object {[propName]: propConfig}. For each specified propName will be created a control on the panel, and the prop value will pe passed to the target component.

The functions that can create propConfig objects are available in Demo.props, and described bellow in this docs.

Available Demo.props

props.string(initialValue)

Creates a string input in the panel.

javascript
import Demo, {props as P} from 'react-demo'

props.number(initialValue)

Creates a number input in the panel.

javascript
import Demo, {props as P} from 'react-demo'

props.text(initialValue)

The same as props.string but uses a textarea as the control, which allows us to input multiline strings.

javascript
import Demo, {props as P} from 'react-demo'

props.bool(initialValue)

Creates a checkbox on the panel.

javascript
import Demo, {props as P} from 'react-demo'

props.choices(options, [initialValue])

Creates a select input in the panel. The options argument can be either an array of available options, or an object {[label]: value}.

Note that we can pass objects of any type in options no matter if we use array or object. When array is used the values will be stringified before using as labels in the control.

Acepts optional initialValue as a second argument. By default the first option is used as the initial value.

javascript
import Demo, {props as P} from 'react-demo'

props.shape(props)

Creates a nested object properties configuration which can include any property configuration from this list.

javascript
import Demo, {props as P} from 'react-demo'

props.json(initialValue)

Allows us to input arbitrary json as a prop value. Uses enhanced textarea as a control.

javascript
import Demo, {props as P} from 'react-demo'

props.constant(value)

Allows us set a constant value for a prop. Doesn't create a control on the panel.

javascript
import Demo, {props as P} from 'react-demo'

props.callback.log([mapFn])

Creates a log widget in the panel, and passes a callback to the target component. Accepts optional mapFn that allows us to transform callback args before they will be shown in the log. Usable when we don't want to see huge event objects in the log for instance.

javascript
import Demo, {props as P} from 'react-demo'

props.callback.logLatest([mapFn])

Same as callback.log, but shows only the last item in the log. Usable if callback may be called too frequently.

javascript
import Demo, {props as P} from 'react-demo'

Advanced mode

The advanced mode allows us to create more complex demos. To use advanced mode instead of specifying target prop, pass a custom render function as a child of Demo.

The custom render fucntion accepts props as the first argument. The following two snippets are equivalent.

javascript
// Normal mode

// Same in advanced mode

  {props => }

In the advanced mode we also get access to the update function, using which we are able to update the demo props' values. The update function is passed as a second argument to the custom render function. We're supossed to call update() similary to setState() — passing an object with props that we want to change.

javascript

  {
    (props, update) =>
       {update({value: e.target.value})}}
      />
  }

Issues· 14 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言