-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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
A global CLI for running a single component #645
Comments
@gaearon I liked that. I guess it wouldn't work for the case where a component looks at a URL and figues something based on that, correct? Or, maybe it would be possible to pass the location prop through the CLI as well? |
What about the props passed to the component ? Where would they be defined ? I think that's what would make the tool worth to use or not. Ideally a UI to tweak the props in realtime :-) |
@tugberkugurlu you could have a defaultProp in place of a url that's passed in? |
Good idea! Besides all the benefits @gaearon said, it could also be very useful to beginners in react. They could go right into creating a new component and understand quickly how react works. Would it work with component that have children components too? Do children have to be in the same folder as the root component?
|
Child components and CSS would be importable relatively, just like they are in Create React App. |
Great initiative ! run-react-component App.js --prop1=foo --prop2=bar |
Not sure why there's need to specify props as CLI when you can do this in JS! Simpler and doesn't require restarting when props change. |
@gaearon then isn't your component starting to look like a standalone app ? |
@dperetti It's not too much extra work to support exporting a React element once you have functionality for supporting exported components in place. @andreypopp contributed this exact functionality to react-heatpack and it worked nicely. |
@insin with functional components this is probably not needed anymore:
|
@andreypopp @insin That would mean having a webpack dev server running? Or is it possible without? It's a little big heavy to "just" prototype a single component no? |
You can specify props with For example you can create |
Not sure what you mean by "heavy". Yes it would run the same development server as CRA itself under the hood. |
Do we really need another project for that? Maybe it is better to rename CRA to
|
@gaearon Got it. I didn't understand it was CRA under the hood the first time you answered me, my bad :) @andreypopp renaming CRA is not a good idea to me since it is pretty recent. That would confuse people :/ |
I'm not a big fan of forcing that Maybe another way would be to have a convention like you have to define a Pushing initial idea a bit, I think if you can render a component standalone like this, another nice property is you could also jest-snapshot-test it automatically! We could have the jest snapshot feature for free, config-less, without ever have written a single line of test. |
I think this is really tool where it helps to onboard a lot of new users to React. I think this is a smaller version of React Storybook |
By 'run a component' - do you mean - invoke its render method once, and print what's returned? |
This is a great idea! I had something similar in a project I was working on, but it was implemented as a separate webpack build that rendered the desired component into a full screen container (we were aiming to create a fully responsive experience). I found that this was great not just for isolating components, but it was actually really helpful for styling shared components because it helped enforce keeping layout/positioning styles where they belonged. Doing this with a CLI is a much nicer experience. As @arunoda mentioned, this seems similar in nature to React Storybook and its ClojureScript predecessor, devcards, so it would probably be good to examine libraries like these to see the current state of the art and gain insight they may have learned along the way. The most interesting problem I see right now is determining how to pass props (or even context) to a component. Off the top of my head we could have some JSON defined in a {
default: {
prop1: value,
prop2: value
},
loading: { ... },
withMaxCount: { ... }
} Then |
cc @frantic |
I had a prototype of this for React Native: depending on the file extension ( |
The confusing part here is people will start trying to do |
I think the best way to handle props with this would be just making a small new file and running that. With this sort of script it should be easy to have multiple entry points in a huge project. So if you really want to see
and Also, if we can really strip down the requirements to a single JavaScript file, there might be some interesting possibilities of finding a subset of features that works in multiple development environments. The initial use case I was thinking about for this whole thing was teaching people React through websites similarly to what we are doing on the React Native website. If you could teach people via something like codepen, where you build an app with just a single file, and the same exact thing is runnable locally, that seems like a nice way to learn. If the codepen app layout is different from the layout of a real application, it's somewhat frustrating to learn it one way and then port your knowledge over. React Native is an interesting case too - if we could make the tooling very similar between React and React Native init for noobs, it could be easier to learn one and port your knowledge to the other. |
I think documenting Storybook integration might address this better than us building a dedicated tool. Closing in favor of #738. |
This is something @lacker was working on before Create React App. That project got shelved but I think we could revive it in some form now.
The idea is that instead of creating a project, you just start with a single file that is the root component:
App.js
It would use the same infrastructure as Create React App (exact same Babel and Webpack settings and CLI output), but would work in any folder without
npm start
.So you could
run-react-component NavBar.js
and see just that component, regardless of where it is in the project, and whether it is in a project or not. The onlyrun-react-component
requirement is that whatever youimport
(e.g.react
) exists innode_modules
.Benefits of this approach:
run-react-component
right away in any folder on the disk. (The downside is you actually have to update global CLI to get updates.)What do you think? Would that be useful?
The text was updated successfully, but these errors were encountered: