-
Notifications
You must be signed in to change notification settings - Fork 64
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
With/let/def statement: define variables (suggestion/enhancement) #58
Comments
I see the use of it in a big template, but in React what's the advantage of using this over refactoring the |
Well maybe I'm doing it wrong but I'm not a huge fan of creating lots of small components. If I have methods with logic that depend on data on the parent, I'd have to pass those down as props. (Convoluted reason, but it's the case in this real-life example I used above.) Then, putting an extra component in the same file is bad form but creating a new file for something like this makes things harder to find. And it feels just wrong to me to have a component that will only ever possibly be used in one place. But mostly, it's the methods thing. And state, and whatever locals I already have at that point. As a rule, if I'm splitting code out into a new component, and I'm passing more than 3 or 4 props, I feel I'm doing it wrong, and try to find a better way. Ironically, as I write, I'm doing exactly that 😸 I just split one component in 3 because the render method was too long, and ended up passing way too much stuff down. So, there are cases and cases, I suppose. What I have in mind here is that the block of code really logically/semantically belongs in the same component, but at the same time I only want to call getSummaryFields() once, because it's somewhat costly. |
In my opinion it's kind of best practice to create as many components as possible. It has been pointed out by others, however, that folders with many, many components are quite messy. In the end I think it's a typical problem of one's folder structure... On the other passing props is just a routine piece of work, but it doesn't add extra complexity (so I think this argument is only about lazyiness vs comfort). In your particular case I think the best way to go forward is to create an own component, as @AlexGilleran suggested. The new "component" you propose is definitely doable, but I don't see the use case. I've never been in a situation where I wanted to set a new variable in the midst of the template... |
It might be worth pointing out here that jsx-control-statements isn't intended as a templating language to enable big I think this is pretty similar to what https://github.com/wix/react-templates does with |
Sometimes I find the ability to use a local assignment within JSX usefull to make my code cleaner. Not very often, but it's good to have the option available. I made a fork with the Usage of the <For each="item" of={this.props.items}>
<With value={computeValue(item)}>
<Choose>
<When condition={item.path}>
<Link to={item.path}>{value}</Link>
</When>
<Otherwise>
{value}
</Otherwise>
</Choose>
</With>
</For> I can extend eslint-plugin-jsx-control-statements as well, so eslint will know what variables been defined. |
That's really cool, but yeah - I think a separate package is appropriate - it's not really a control statement as such. What do you think @texttechne ? Will definitely be happy to link to it from our Readme :) |
Nice work. And now I understand the use case. @AlexGilleran: I think, I would include it as part of @martinmacko47: The way you've designed the API of the with statement is absolutely fine on its own and very flexible. You can define multiple values in one go: |
I kind of had this discussion with myself in the OP 😺 In isolation, clearly I prefer the way @martinmacko47 did it, in fact exactly the way he did it. But to make it more consistent and therefore easier to learn, I proposed an alternative there, with a single attribute, One could read that and expect they could use also a Well, I suppose it could compile to In that spirit I think |
Cool beans, I'm sufficiently on the fence about this be swayed towards having it as a part of JCS :). I actually really like the |
I'm glad you are starting to like the new control statement. I'd prefer adding it to JCS, because if I created a separate package for As for the syntax, I'm not sure which one is better. Pros of
Pros of
Btw, I wouldn't name the attribute |
@AlexGilleran @texttechne Should I create a PR? |
Sure, sorry. I prefer the key={value} syntax but I'm happy with a PR that has either :). |
@AlexGilleran So I created PR #63 with I'll add |
@AlexGilleran I've created a PR in eslint-plugin-jsx-control-statements to treat variables defined in |
Btw, I know nothing about FlowType, so please somebody update jsx-control-statements.flow.js |
@AlexGilleran No activity with my PR in eslint-plugin-jsx-control-statements in two weeks. Should I create a PR to update our readme now, or should we still wait for eslint-plugin-jsx-control-statements? |
Yeah just update the readme, ESLint can wait :). If it stays inactive we might have to fork it. |
@AlexGilleran Ok. So here is PR #64 with extended readme. |
:D |
Will push a new version to npm a bit later :). |
And published! Thanks @martinmacko47 😁 |
@AlexGilleran Great! Now, let's hope eslint plugin gets eventually updated as well. |
FYI: eslint-plugin-jsx-control-statements was updated today to support |
There's this one piece of code where I want to run a method once and bind its result locally. It looks more or less like this:
Kind of makes my eyes bleed, and it's hard to see where the “variable” comes from.
(Sorry if the example is not as minimal as possible, but I wanted to illustrate why I want it to begin with; if it wasn't inside a
<For>
I could just define it as aconst
on top of the render method.)Drawing from the ancient template languages of my young days…
(Or you could call it
<With>
or<Def>
or a number of other things. Not<Const>
though, that would make it look like it's doing something completely different.)A possible objection is that using prop names this way is iffy, and I guess some are already uncomfortable with the
each
andindex
props in<For>
. An alternative, arguably more React-y:It would expand of course to pretty much what I have on top, an in-place function, except no need for trickery with passing things as arguments.
Known issue: expanding it that way means it's limited to containing exactly one child.
The text was updated successfully, but these errors were encountered: