Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

react-view

> 前端框架
Open source

React View is an interactive playground, documentation and code generator for your components.

710 stars0 likes1 views
WebsiteGitHub

About

React View is an interactive playground, documentation and code generator for your components.

React View is a set of tools that aspires to close the gap between users, developers and designers of component libraries. React View aims to make documentation more interactive and useful. It utilizes already popular tools such as babel and prettier. Read the introductory blog post.

See the example. CodeSandbox.

There are a few ways how to use React View:

  • All-in-one playground. Import ``, give it the component configuration and drop it in your web documentation. This is ideal if you want to start as quickly as possible and don't need to customize anything.
  • Build your own playground. Import the useView hook and give it the component configuration. This hook handles the playground state and returns various props and callbacks that you can fit into your own UI components. React View also exports all default UI parts separately (Editor, ActionButtons, Compiler, Knobs, Error...) so you can reuse them. This is a great option if you want to customize some parts of the default UI (or all of it) without worrying about the rest.
  • Live code editing only. Sometimes it is useful to only have editable source code and live preview without the list of props. You can use useView for that too. Just don't give it any component configuration and don't render the `` component. React View uses babel, so you can add additional presets and enable TypeScript.
  • VS Code Snippets. Leverage the component documentation to programatically generate VS Code snippets to make your developers more productive. More information.

Installation

bash
yarn add react-view

All-in-one Playground

typescript
import {View, PropTypes} from 'react-view';
import {Button} from 'your-button-component';

export default () => (
   alert("click")',
        type: PropTypes.Function,
        description: 'Function called when button is clicked.',
      },
      disabled: {
        value: false,
        type: PropTypes.Boolean,
        description: 'Indicates that the button is disabled',
      },
    }}
    scope={{
      Button,
    }}
    imports={{
      'your-button-component': {
        named: ['Button'],
      },
    }}
  />
);

This is a basic example demonstrating the all-in-one View component. You need to define the component name, props, scope and imports. This example renders the gif above (except the size prop).

Build Your Playground

Bring your own UI. The View component is a tiny wrapper around the useView hook. If you want to customize and have more control, you can use the useView hook directly. You can still re-use our default UI components or create your own:

…

See the example.

Live Code Editing Only

See the example.

If you don't need the code generation or props documentation, you can also use React View for editing source code with a live preview.

typescript
import {useView, Compiler, Editor, Error} from 'react-view';
import presetTypescript from '@babel/preset-typescript';

export default () => {
  const params = useView({
    initialCode: `() => {
      const text: string = "Hey";
      return 
{text}
;
    }`,
    scope: {},
    onUpdate: console.log,
  });

  return (
    
      
      
      
    
  );
};

This example also demonstrates how to opt-in into TypeScript support. Since we use babel as the compiler, you can simply add additional presets (preset-react is applied by default). If you are using additional components or dependencies, you need to pass them through scope.

It can also compile a naked JSX element:

typescript

Hey

Or pretty much anything that can be executed after the return statement of JavaScript function:

javascript
2 + 5;

Code Snippets

VS Code has a powerful feature called code snippets. It lets you to quickly embed and modify repetitive code. In our case, React components:

However, first you need to define these code snippets with a special grammar:

…

This would be normally a lot of error-prone work. Fortunately, you can auto-generate this based on the same component configuration you already use for the React View playground.

javascript
import {vscodeSnippet, PropTypes} from 'react-view';

const snippet = vscodeSnippet({
  prefix: ['Button component'],
  componentName: 'Button',
  props: {
    children: {
      value: 'Hello',
      type: PropTypes.ReactNode,
      description: 'Visible label.',
    },
  },
  imports: {
    'your-button-component': {
      named: ['Button'],
    },
  },
});

// then you might want to write it into a file so it can be loaded into VS Code
fs.writeFileSync(
  path.join(__dirname, 'components.code-snippets'),
  JSON.stringify({Button: snippet}, undefined, ' ')
);

Snippets can be used locally or bundled with an extension. Check the documentation. Also there is an article about how we use this feature at Base Web.

Applications

We built this library for ourselves as a part of baseweb.design documentation (uber/baseweb). We use all features of React View extensively. Our playground components are customized reusing existing Base Web components. We have implemented an entire custom playground section called Style Overrides and there is also support for theming through a context provider.

See the example.

API Documentation

useView Arguments

javascript
import {useView, PropTypes} from 'react-view';

useView accepts a configuration object where all values are optional:

componentName

typescript
componentName?: string;

The name of the primary component in the playground. It's used for the code generation and props parsing.

imports

typescript
imports?: {
  [key: string]: {
    named?: string[];
    default?: string;
  };
};

Optionally, the code generator can output import statements at the top of code editor so you can make the examples fully copy pastable. key represents the name of a module. Each module can have a single default and multiple named imports. For example:

javascript
imports: {
  'baseui/button': {
    named: ['SIZE'];
    default: Button;
  };
};

would generate

javascript
import Button, {SIZE} from 'baseui/button';

scope

typescript
scope?: {[key: string]: any};

All outside dependencies in the code editor need to be explicitly passed through the scope. Why? imports do nothing. Their only purpose is to make copy-pasting easier. React View does not include a bundler, so it can't understand ES modules. For example, if you want to use styled function from the styled-components in the code editor, you would do:

javascript
import styled from 'styled-components';

useView({scope: {styled}});

React is included by default.

props

typescript
props?: {[key: string]: {
  value:  boolean | string | number | undefined;
  type: PropTypes;
  description: string;
  options?: any;
  placeholder?: string;
  defaultValue?: boolean | string | number | undefined;
  enumName?: string;
  hidden?: boolean;
  imports?: {[key: string]: {
    named?: string[];
    default?: string;
  }};
  stateful?: boolean;
  propHook?: {
    what: string;
    into: string;
  };
  custom?: any;
}};

If you wish to use the code generation and knobs you need to define the component's API - props. Each prop can take multiple options:

  • value - The default value (initial state).
  • type - React View has some predefined internal types that are used to generate and parse code and should describe any React component: String, ReactNode, Boolean, Number, Enum, Array, Object, Function, Ref, Date or Custom.
  • description - Displayed as a knob tooltip for the user.
  • options - When type is set to PropTypes.Enum you need to pass the enum through options so React View can generate all options (as input radio or select).
  • enumName - When type is set to PropTypes.Enum, you need to pass the enum name through enumName so the code generator knows what to output.
  • placeholder - A value placeholder, displayed in knobs.
  • defaultValue - Can be useful for PropTypes.Enum or PropTypes.Boolean. When value equals defaultValue, the prop will not be code generated. It keeps examples with set default options cleaner.
  • hidden - Some components have a lot of props but not all of them are that useful. You can optionally hide them from the default list of knobs. The user still can expand them via the "Show all props" button.
  • imports - Same as the top-level imports argument. However, it will add import statements only if the prop is being used and code generated. For example, there is no need to display the enum import if the related prop is not used (defaultValue). It keeps examples cleaner.
  • stateful - It detaches the prop value into an internal React.useState hook. This can make components like inputs interactive. More info.
  • propHook - This works in combination with the stateful flag. More info
  • custom - When you define customProps, you can use this part of the configuration to pass arbitrary data.

onUpdate

typescript
onUpdate?: (params: {code: string}) => void;

Called when the source code changes.

initialCode

typescript
initialCode?: string;

You can set an initial source code. This can be useful in combination with onUpdate to preserve the playground's state through the URL. React View would initially hydrate the state from the initialCode value but all subsequent changes will be driven by its internal state.

If you use useView only as the live code editor, you should always set this value to something so the user doesn't see a blank page.

provider

typescript
provider?: {
  value: T;
  parse: (astRoot: any) => T;
  generate: (value: T, childTree: t.JSXElement) => t.JSXElement;
  imports: [key: string]: {
    named?: string[];
    default?: string;
  }
};

This is an advanced API. It lets you build an arbitrary provider wrapper around your component. See the example.

  • value - Can be anything, represents the state of provider, when undefined the provider is not code generated.
  • parse - Gives you the AST root, you should return the value so React View knows how to parse out the provider value out of the source code.
  • generate - Gives you the value (provider state) and inner component (as a t.JSXElement), you should return the AST of your provider including the inner component so React View knows how to turn the provider value into the source code/
  • imports - Same as the top-level imports, only displayed when the provider is being code generated (value is not undefined).

customProps

typescript
customProps?: {
  [key: string]: {
    parse: (code: string, knobProps: any) => any;
    generate: (value: any) => any;
  }
};

This is an advanced API. It lets you build your own prop/knob types and UIs. See the example. First, you set the type of your prop to PropTypes.Custom. Then, you can target it by its name (key is the prop name). For each prop, you need to define:

  • parse - Gives you the prop code and the prop configuration (a part of the props list), you should return the value (in

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

TypeScriptcode-generatordocumentationplaygroundreact

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category前端框架
PricingOpen source

> Related tools

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架