GithubHelp home page GithubHelp logo

Comments (8)

scamden avatar scamden commented on June 30, 2024 29

@adidahiya i'm curious how you handle simple conditional templates without multiline jsx?

for example:

{
variableThatCouldBeUndefined && 
  <div>
    <span>
     {variableThatCouldBeUndefined}
    </span>
  </div>
}

I started trying to refactor these kinds of things into functions, but i found that for simple cases like this it actually made the code much less readable as it scattered the pieces all over the file. I'm curious how you handle these and similar cases with this rule on.

from tslint-react.

adidahiya avatar adidahiya commented on June 30, 2024 21

@scamden yeah, I'd say that's fair criticism for this rule and might warrant a new rule option to allow simple conditional templates (limit the conditional expression itself to one line, otherwise this is easily abused).

currently, I end up with a lot of code like this:

render() {
  return (
    <div>
      {this.maybeRenderA()}
      {this.maybeRenderB()}
    </div>
  );
}

private maybeRenderA() {
  if (foo) {
    return ...
  }
}

private maybeRenderB() {
  if (bar) {
    return ...
  }
}

... I personally don't mind it too much but it is a stylistic preference that not everyone's going to agree with.

I mostly didn't like seeing a lot of JSX code like this:

{this.props.items.map((i: Item) => (
    <ItemComponent ... />
))}

from tslint-react.

vyushin avatar vyushin commented on June 30, 2024 21

I just disabled it :)

{
  "extends": ["tslint:recommended" ,"tslint-react"],
  "rules": {
    "jsx-no-multiline-js": false
  }
}

from tslint-react.

orta-sanz avatar orta-sanz commented on June 30, 2024 2

What is your alternative to this? Is there a better way that I am unaware of?

{this.props.items.map((i: Item) => (
    <ItemComponent ... />
))}

Yes, disable the rule and be happy

from tslint-react.

AlexReff avatar AlexReff commented on June 30, 2024 1

What is your alternative to this? Is there a better way that I am unaware of?

{this.props.items.map((i: Item) => (
    <ItemComponent ... />
))}

from tslint-react.

scamden avatar scamden commented on June 30, 2024

ya that could be cool to limit the scope of it slightly. i also had some terrible nested conditionals that were all scoped to one expression at the top level so i can see the desire to prevent that. but ya seems like a middle ground would be helpful

from tslint-react.

leoskyrocker avatar leoskyrocker commented on June 30, 2024

saying maybeA and maybeB is very different from saying if A else B
maybeA and maybeB means both can render at the same time.

from tslint-react.

Alecell avatar Alecell commented on June 30, 2024

This rule is most for readability than another one reason and its awesome, I just get a legacy project with nested multiline jsx which takes the code readability to trash. I just want to say that since most people criticized that rule and IMO this rule is awesome.

from tslint-react.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.