Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code that I have to write in examples is different from "real" React code #324

Closed
n1313 opened this issue Feb 14, 2017 · 19 comments
Closed

Comments

@n1313
Copy link
Collaborator

n1313 commented Feb 14, 2017

Code examples are very useful as a part of documentation. They allow the reader to see how the "theory" of a component is applied in practice, and are the most useful when they can be easily put into a real application. With react-styleguidist, this works quite well in many cases: you write your code exactly as it should be written and your reader can just copy-paste it into their app and get it working.

Unfortunately, it doesn't work for more complex components that require some sort of external "scaffolding" to work, or for complicated setups, for example, with HOCs and theming. RSG has initialState and setState which work great but cannot be copy-pasted into a real React application: they have to be rewritten into some sort of constructor method and this.setState calls.

Don't get me wrong here though, I'm not saying that RSG is buggy or does its job poorly. It works great, I'm quite happy that I don't have to write all the import React from 'react'; fluff around most of my components most of the time and can just plop a short JSX one-liner and see it "just work". The task of rendering arbitrary code is hard, and it is even harder if you want to have some sort of magical balance between writing short code and writing complicated code. But it would be great if the code in my examples was closer to what my users have to write in their projects.

I don't have a solution at hand and I don't even know if there is a solution for this issue, given all the restrictions that RSG has. So let's talk about this, maybe? Does anybody here have this problem, and how do you deal with it? Is there anything that could be added to RSG to make it even better in this regard?

@sapegin
Copy link
Member

sapegin commented Feb 14, 2017

I don’t know a good way to solve this. So far I use comments and define component classes inside examples:

/*
import TableProvider from 'kao/components/TableProvider';
import * as sort from 'kao/util/sort';
import { Header, Body } from 'reactabular-table';
import orderBy from 'lodash/orderBy';
*/
class SortTable extends React.Component {
  constructor() {
    super();
    const sortable = sort.sort({
      getSortingColumns: () => this.state.sortingColumns,
      onSort: selectedColumn => {
        this.setState({
          sortingColumns: sort.byColumn({
            sortingColumns: this.state.sortingColumns,
            selectedColumn
          })
        });
      }
    });
    this.state = {
      sortingColumns: {},
      columns: [
        {
          property: 'name',
          header: {
            label: 'Name',
            formatters: [sort.header(sortable)],
          },
        },
        {
          property: 'color',
          header: {
            label: 'Color',
            formatters: [sort.header(sortable)],
          },
          cell: {
            formatters: [fixtures.getColorName],
          },
        },
        {
          property: 'male',
          header: {
            label: 'Male',
            formatters: [sort.header(sortable)],
          },
          cell: {
            formatters: [male => male && <Icon type="check" />],
          },
        },
        {
          property: 'weight',
          header: {
            label: 'Weight',
            formatters: [sort.header(sortable)],
          },
          cell: {
            formatters: [weight => `${weight} g`],
          },
          props: {
            className: 'text-right',
          },
        },
      ],
      rows: fixtures.rows,
    };
  }
  render() {
    const { columns, rows, sortingColumns } = this.state;
    const sortedRows = sort.sorter({
      columns,
      sortingColumns,
      sort: orderBy
    })(rows);
    return (
      <TableProvider columns={columns}>
        <Header />
        <Body rows={sortedRows} rowKey="id" />
      </TableProvider>
    );
  }
}
<SortTable />

image 2017-02-14 at 11 20 32 am

@okonet
Copy link
Member

okonet commented Feb 14, 2017

@sapegin yeah, that's annoying. Can we create a custom babel plugin that would strip off imports in examples if not needed?

@sapegin
Copy link
Member

sapegin commented Feb 14, 2017

There’s no Babel on the frontend. And I think this behavior would be very confusing because for example you can use require in example and it will work.

@okonet
Copy link
Member

okonet commented Feb 14, 2017

There’s no Babel on the frontend.

So, how is the rest of the code being transpiled to ES5?

you can use require in example and it will work

Oh, probably I misunderstand the problem then. Why is import and export aren't working then?

@sapegin
Copy link
Member

sapegin commented Feb 14, 2017

So, how is the rest of the code being transpiled to ES5?

With Buble. We can’t use Babel because it’s way too huge.

Oh, probably I misunderstand the problem then. Why is import and export aren't working then?

Because require is just a function that returns a module, included in the bundle on the build step.

@okonet
Copy link
Member

okonet commented Feb 14, 2017

Gotcha. Thanks for clarifying.

@roychoo
Copy link

roychoo commented Feb 14, 2017

though it might not be the best way, and it might print relative path instead of the modules path that client is going to write.

it seems that https://github.com/nerdlabs/react-docgen-imports-handler will put the imports in the documentation, and maybe comment the block so that buble doesn't throw errors??

@sapegin
Copy link
Member

sapegin commented Mar 31, 2017

Can we do anything here? Update the docs or something for example? If not I’d close it.

@stereobooster
Copy link

Any plans to support import in the code instead of require()? Related: https://gitlab.com/Rich-Harris/buble/issues/59

@sapegin
Copy link
Member

sapegin commented May 13, 2017

@stereobooster We can discuss it as soon as Buble implements them ;-) So far I don’t see we can do anything here.

@sapegin
Copy link
Member

sapegin commented Jul 14, 2017

Try the new 6.0.0 beta — it may solve some of this issue.

@n1313
Copy link
Collaborator Author

n1313 commented Jan 4, 2018

@sapegin could you please explain this a bit? I read the release notes but I didn't see how it is related.

@sapegin
Copy link
Member

sapegin commented Jan 4, 2018

I guess I meant importing examples from files.

@n1313
Copy link
Collaborator Author

n1313 commented Jan 4, 2018 via email

@sapegin
Copy link
Member

sapegin commented Jan 4, 2018

There’s one in my comment at the very beginning of this issue ;-)

@n1313
Copy link
Collaborator Author

n1313 commented Jan 4, 2018 via email

@n1313
Copy link
Collaborator Author

n1313 commented Jan 9, 2018

It works! Wow. Very nice. Is this documented anywhere?

@sapegin
Copy link
Member

sapegin commented Jan 9, 2018

I'm not sure, in any case worth documenting it more ;-)

@n1313
Copy link
Collaborator Author

n1313 commented Jan 10, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants