最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ReactJS - How to change style and class of react component? - Stack Overflow

programmeradmin3浏览0评论

I'd like to be able to change the style and className of a component before it's rendered, outside of it's render function. I've got more going on than I'm showing here, but this is the basic idea, being able to set the style and className as properties somehow:

The following works only if the "style" variable is moved inside the render function, and added to the div like normal (e.g. <div style={style}> ). How can I make the following work?

JS Fiddle that doesnt work

EDIT: Working JS Fiddle here !

/** @jsx React.DOM */

var Main = React.createClass({

    render: function() {
       var result = this.doRender();

       var style = {
         border:'1px solid red'
       };

       result.style = style;

       return result;
    },

    doRender: function() {
        return (
          <div>Test</div>
        );
    }
});

React.renderComponent(<Main/>, document.body);

I'd like to be able to change the style and className of a component before it's rendered, outside of it's render function. I've got more going on than I'm showing here, but this is the basic idea, being able to set the style and className as properties somehow:

The following works only if the "style" variable is moved inside the render function, and added to the div like normal (e.g. <div style={style}> ). How can I make the following work?

JS Fiddle that doesnt work

EDIT: Working JS Fiddle here !

/** @jsx React.DOM */

var Main = React.createClass({

    render: function() {
       var result = this.doRender();

       var style = {
         border:'1px solid red'
       };

       result.style = style;

       return result;
    },

    doRender: function() {
        return (
          <div>Test</div>
        );
    }
});

React.renderComponent(<Main/>, document.body);
Share Improve this question edited Mar 18, 2016 at 16:05 Brad Parks asked Jul 20, 2014 at 23:57 Brad ParksBrad Parks 72k70 gold badges285 silver badges367 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 17

React elements are designed to be immutable; usually your app will be easiest to understand if you restructure it to build the proper props upfront instead of mutating them later, and React assumes that this is the case. That said, you can use React.cloneElement to get the effect you want:

render: function() {
    return React.cloneElement(this.doRender(), {
        style: {border: '1px solid red'}
    });
},

(Note that if your doRender() function returned a custom component then changing the props would change that component's props, not the underlying DOM component that gets produced. There's no way to render it down to a DOM component and change that component's props, short of manually mutating the DOM in componentDidMount.)

you can try this game

componentDidMount() {
  document.querySelector('.yourClassName').style = 'your: style'
}
发布评论

评论列表(0)

  1. 暂无评论