question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Variables: build-time vs run-time

See original GitHub issue

I think it’s great that react-native’s StyleSheet and this library both let us do run-time branching of styles in the same place we write our styles normally. I think it is a shame to lose the ability to do build-time abstractions. StyleSheet still lets you plug in variables at definition time, so they don’t lose build-time configurability, but I don’t see a way to do that with styled-components.

What do I mean?

On the web, I use SASS/LESS for build time abstraction. If I want a run-time abstraction I have to do some messy gluing to JS. At the very least the JS had to know about a css classname.

In styled-components, only run-time branching is supported:

const WarningMessage = styled.li`
  padding: 5px 10px;
  background-color: ${props => props.theme.colorWarn}; // color variable, could be a build-time decision
  color: 'white';
  box-shadow: 1px 2px 2px 0 ${props => props.theme.shade10 }; // color constant, could be a build-time decision;
  font-weight: ${props => props.important ? '"bold"' : '"normal"' }; // run-time decision, based on data about this particular message
  border-radius: ${props => props.theme.radiusSmall} // build-time would be better
`;

Use cases for run-time abstractions:

  • User or data influenced component state (eg: a todo li that is done or not)

Better fit for build-time abstraction:

  • button that is primary or not (since it’s unlikely to change as a result of user input)
  • Color variables
  • Most applications of theming (counterargument: user selectable theme)
  • UI constants (spacing, radius, some types of color definitions)

I’d argue that most of the use cases are build-time. Doing it all at run time means there is a perf impact (may be trivial) and there’s a larger possibility for unexpected values to break my code.

I wonder: Is there a way to support both run-time and build-time branching? Is this a use case others find interesting?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
geelencommented, Mar 15, 2017

Yeah, I think conceptually the styling block must be completely run-time. We can make build-time optimisations (like the babel step, ripping out the parser, server-side-rendering and rehydration, etc) all within that conceptual framework, as any performance issues are identified. But I don’t want to introduce any new concepts that force a user to make a build-time/run-time decision, sorry.

0reactions
hitmandscommented, Oct 5, 2017

something like that could be achieved via Prepack

Read more comments on GitHub >

github_iconTop Results From Across the Web

Build vs Runtime environment variables - Stack Overflow
An environment variable that would only be set for the deployment is where a database used by the application can be found. This...
Read more >
Docker Tip #47: Build Time vs Run Time ENV Variables
Docker allows you to set both build time and run time ENV variables and even lets you overwrite build time ENV vars at...
Read more >
Create React App: Run-time vs Build-time Environment ...
This post will show you how I migrate create-react-app custom environement variables from build-time to be configurable at run-time.
Read more >
Development time vs build time vs runtime - Medium
At development time we set things like constants that we cannot change. On the other hand, when we shift from development time to...
Read more >
Runtime variables vs build time variables in ReactJS : r/devops
You are a 100% right with setting runtime variables rather than build time. We are trying to implement that as well.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found