1

I am trying to put two variables inside a style block in Reactjs. How do you go about doing this?

var GlobalStyles = require(./jsStyles/GlobalStyles.js);
var moduleSpecificStyles = {
    hide:{
        'display':'none'
    },
    //more styles
};

...

render:function(){
    return (
        ...
        <div style={GlobalStyles.container, moduleSpecificStyles.hide}></div>
    )
}

This does not seem to work. It will work on either one if I only have one in. But adding both, I am not sure how to get it working.

2 Answers 2

1

You need to compose style objects together, if you're using ES2015 then you can use Object.prototype.assign function.

<div style={Object.assign({}, GlobalStyles.container, moduleSpecificStyles.hide)}></div>

No matter whether your GlobalStyles.container object has a display: block property value or not, the composed style object will have display: none.

Sign up to request clarification or add additional context in comments.

Comments

0

The style prop expects a single object with key value pairs. See docs for more.

If you want to combine the two, then use Object.assign mdn docs

render() {
   var style = Object.assign({}, GlobalStyles.container, moduleSpecificStyles.hide)
   return <div style=={style}></div>
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.