javascript - Why are props being shared between seperate instances of an emotion-js component?


Question: 

  1. Why does emotion-js appear to share the props from seperate component instances?
<Button isDisabled={'true'} />
<Button />
const Button = styled.button`
  background-color: ${props => props.isDisabled !== 'true' ? 'green' : 'grey'};
`;
export default (props) => {
    return (<Button>Hello</Button>);
}

Expectation: The first <Button/> is grey. The second is green.

Reality: Both instances of <Button/> are grey.


Also, this one had me very confused until I noticed this snippit from the docs:

By default, Emotion passes all props to custom components and only props that are valid html attributes for string tags.

  1. Why pass only props that are valid string tags? The following use of bool seems like it would be a valid use case.
<Button isDisabled={true} /> 



1 Answer: 

It does not share props. You just need to pass them properly. There are two ways to achieve what you want, either do it with proper strings or with boolean values:

String version:

const Button = styled('button')`
  background-color: ${props => props.isDisabled !== 'true' ? 'green' : 'grey'};
`;

Usage:

<Button isDisabled="true">grey</Button>
<Button>green</Button>

Or with bool values (what might be preferable in this case):

const Button = styled('button')`
  background-color: ${props => props.isDisabled ? 'grey' : 'green'};
`;

Usage:

<Button isDisabled>Grey</Button>
<Button>green</Button>
 

More Articles


R: turning list items into objects

I have a list of objects that I've created manually, like this:rand1 <- rnorm(1e3)rand2 <- rnorm(1e6)myObjects <- NULLmyObjects[[1]] <-rand1myObjects[[2]] <-rand2names(myObjects) <- c("rand1","rand2")I'm working on some code that bundles up objects and puts them up in S3. Then I ha

macos - uninstall python on mac Mavericks

I'm a new Mac user. I installed python 2.7.6 but I cannot run my scripts (through IDLE or simply a script). I want to reinstall it but I don't want to mess with the version which comes pre-installed on mac.Thank you for your help.note: there are questions on stackOverflow that are similar to mine, y

php - how to set a page active automatically when it is loaded

i want to change the active class when each page loaded. my jquery code is$(document).ready(function () {$('.nav li a').click(function(e) { $('.nav li a.active').removeClass('active'); $(this).addClass('active'); e.preventDefault();});});and my html code is<ul class="nav nav-stacked bg-n


reactjs - What is the benefit of @emotion/core over emotion for a React project?

If I install emotion then the API is nice and clear: package.json:"dependencies": { "emotion": "^10.0.9",React component: import React from "react";import { css } from "emotion";const someStyle = css` display: none;`function MyComponent() { return ( <div className={someStyle} /> );}Ho

specifications - Essential techniques for pinpointing missing requirements?

An initial draft of requirements specification has been completed and now it is time to take stock of requirements, review the specification. Part of this process is to make sure that there are no sizeable gaps in the specification. Needless to say that the gaps lead to highly inaccurate estimates,

mysql - PHP-FPM crashes when having too many users while doing a heavy job

I have a Server running Apache/2.2.22 (Debian), PHP 5.6.17 as FPM and MySQL 5.6.25.The project runs using a CMS called Redaxo (I don't think it's that important, but I'll tell ya anyway). In Redaxo there are some functions which take some time (e.g. deleting cache and rebuilding it takes 1-2 minutes


Java's "forEach" to Scala

How can the following be converted to scala? If I live it as it is, I'm getting a big type mismatch expected.entrySet().forEach(entry -> {..})I tried specifying entry to java.util.Map.Entry, or changing to scala foreach, doesn't work.Let me know if you need any more info/code, and I'll create som

macos - What is the difference between installing python from the website and using brew?

I have a Mac with OSX 10.11.6. I used brew to install python3. It installed python 3.5.2, but I need python 3.5.1. I've been googling, but can't figure out how I would install 3.5.1 via brew. So I went to python.org and downloaded the python-3.5.1-macosx10.6.pkg. I searched for how installing python

java - Hadoop options are not having any effect (mapreduce.input.lineinputformat.linespermap, mapred.max.map.failures.percent)

I am trying to implement a MapReduce job, where each of the mappers would take 150 lines of the text file, and all the mappers would run simmultaniously; also, it should not fail, no matter how many map tasks fail.Here's the configuration part: JobConf conf = new JobConf(Main.class); c

php - Dynamically replace form with message upon submission

I've written a basic PHP script to generate an email when my contact form is submitted, but as it stands, it redirects the user to an error or thankyou page, which is a little clunky. I'd love to dynamically replace the entire form (or, at least, the submit button) with an error or success message.