Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="content" style="margin-bottom: 20px;"></div>
              
            
!

CSS

              
                
              
            
!

JS

              
                /** @jsx React.DOM */
var ComponentBox = React.createClass({
        getInitialState: function () {
            console.log('-------getInitalState execute-------');
            return {
               stateValue: 'zhangmeng'
            }
        },
        getDefaultProps: function () {
            console.log('-------getDefaultProps execute-------')
            return {
                propValue: 'beijing',
                weather: ''
            }
        },
        /**** 生命周期方法 mounting update render Unmounting*****/
        //一般此处获取数据
        componentWillMount: function () {
            console.log('-------componentWillMount execute-------','propValue:' + this.props.propValue, 'stateValue:' + this.state.stateValue)
//            this.setState({
//                stateValue: 'jiangyu'//render前会执行,改变初始值但是不会触发额外的render
//            })
        },
        //什么时候会被调用 已加载组件收到新的参数时调用 Use setState() here 例子??
        componentWillReceiveProps: function (nextProps) {
            console.log('-------componentWillReceiveProps execute-------', 'old weather is this.props.weather:', this.props.weather, 'new props is nextProps.weahter:' + nextProps.weather);
        },
        //组件判断是否重新渲染时调用 Skips render() if returns false
        shouldComponentUpdate: function (nextProps, nextState) {
            console.log('-------shouldComponentUpdate execute-------')
            var flag = true;
//            if (nextProps.weather == 'light rain' ) {
//                flag = false;
//            }
            return flag;
        },
        //不能调用setState
        componentWillUpdate: function (nextProps, nextState) {
            console.log('-------componentWillUpdate execute-------');
        },
        render: function () {
            console.log('-------render execute-------', 'propValue:' + this.props.propValue, 'weather:', this.props.weather, 'stateValue:' + this.state.stateValue);
            return (<div>
                      <input type="text" ref="myTextInput" defaultValue={this.props.propValue} />
                      <input type="button" ref="myButton" value="提交"/>
                      <div>propValue is: {this.props.propValue}</div>
                      <div>ajaxResult is: {this.props.weather}</div>
                   </div>
            );
        },
        //Operate on the DOM here
        componentDidUpdate: function (prevProps, prevState) {
            console.log('-------componentDidUpdate execute-------')
        },
        //可以在这个方法中和其他框架交互、setTimer或者做ajax请求
        componentDidMount: function () {
            console.log('-------componentDidMount execute-------')
            var input = $('input[type="text"]');
            var cityName = this.props.propValue;
            input.focus();
            $.get('http://api.openweathermap.org/data/2.5/weather?q=' + cityName , function (data,status) {
                    if (status === 'success') {
                        var weather = data.weather || [];
                        if (weather.length && weather[0]) {
                            console.log('**** props weather changed *****');
                            this.setProps({
                                weather: weather[0].description
                            });
                        }

                    }
            }.bind(this));
        },
        //Invoked before DOM removal
        componentWillUnmount: function () {
            console.log('-------componentWillUnmount execute-------')
        }
    });
    var compInstance = React.render(
            <ComponentBox />,
            document.getElementById('content') //DOM Element
    );
              
            
!
999px

Console