Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xgqfrms-GitHub/a36b56ac3c0b4a7fe948f2defccf95ea to your computer and use it in GitHub Desktop.
Save xgqfrms-GitHub/a36b56ac3c0b4a7fe948f2defccf95ea to your computer and use it in GitHub Desktop.
class components props & function components (props)

Raect Components props in deepth

https://facebook.github.io/react/docs/react-component.html#setstate

Class Component this.props.onClickFunction !== Function Component props.onClickFunction

this.props 是自己的特有属性,props 除了包含自己所有的属性,还包含从父级组件继承的属性

class components props & this.props.onClickFunction

function components (props) & no need this

// https://facebook.github.io/react/docs/react-component.html#setstate


// class props & this.props.onClickFunction

class PropsTest extends React.Component{
    render() {
        return (
            <div>
                <button onClick={this.props.onClickFunction} >
                    Add counter
                </button>
                <p>
                    this state is 
                    <span 
                        style={{color: "#0ff", fontSize: "32px"}}
                        >
                        {this.props.counter}
                    </span>
                </p>
            </div>
        );
    };
};

// function props & no need this

const Test = (props) =>{
    return (
        <div>
            <button onClick={props.onClickFunction} >
                Add `props` counter
            </button>
            <p
                style={{color: "#0ff", fontSize: "32px"}}
                >
                {props.counter}
            </p>
        </div>
    );
};



class App extends React.Component{
    state = {
        counter: 1
    };
    addCounter = () => {
        this.setState(
          (prevState) => (
              {
                  counter: prevState.counter + 1
              }
          )
        );
    };
    render(){
        return(
            <div>
                <PropsTest 
                    onClickFunction={this.addCounter}
                    counter={this.state.counter}
                    />
                <Test 
                    onClickFunction={this.addCounter}
                    counter={this.state.counter}
                    />
            </div>
        );
    }
}


export default App;

const props = {
    name: "xgqfrms",
    age: 17
};

ReactDOM.render(
    <div>
        <App {...props}/>
    </div>
    , mountNode
);
@xgqfrms-GitHub
Copy link
Author

@xgqfrms-GitHub
Copy link
Author

@xgqfrms-GitHub
Copy link
Author

React multi className

    className="sidebar-btn"
    className={this.props.styles}

@xgqfrms-GitHub
Copy link
Author

xgqfrms-GitHub commented Jun 30, 2017

https://gist.github.com/xgqfrms-GitHub/a36b56ac3c0b4a7fe948f2defccf95ea#gistcomment-2136607

<div style={{
        width: '270px',
        background: '#f0f0f0',
        borderRight: "30px solid red",
        minHeight: ' 500px',
        maxHeight: '700px',
        overflowX: 'hidden',
        overflowY: 'scroll',
    }}
    className="sidebar-btn"
    className={this.props.styles}
    >
</div>

@xgqfrms-GitHub
Copy link
Author

xgqfrms-GitHub commented Jun 30, 2017

CSS 优先级 & React sider style

inline style can't be overwrite by .className

CSS 优先级 inline > style > .css

!important > #id > details path > ...

@xgqfrms-GitHub
Copy link
Author

https://developer.mozilla.org/zh-CN/docs/Web/CSS/Specificity

0. 类型选择器(type selectors)(例如, h1)
和 伪元素(pseudo-elements)(例如, ::before)

1. 类选择器(class selectors) (例如,.example),
属性选择器(attributes selectors)(例如, [type="radio"]),
伪类(pseudo-classes)(例如, :hover)

2. ID选择器(例如, #example)

通用选择器(universal selector)(*), 
组合子(combinators) (+, >, ~, ' ')  
和 否定伪类(negation pseudo-class)(:not()) 对特异性没有影响。
(但是,在 :not() 内部声明的选择器是会影响优先级)。

给元素添加的内联样式 (例如, style="font-weight:bold") 总会覆盖外部样式表的任何样式 ,
因此可看作是具有最高的优先级。.

!important 规则例外
当在一个样式声明中使用一个!important 规则时,此声明将覆盖任何其他声明。
虽然技术上很重要与特异性无关,但它与它直接相关。

@xgqfrms-GitHub
Copy link
Author

xgqfrms-GitHub commented Jun 30, 2017

权值

(外部样式)External style sheet <(内部样式)Internal style sheet <(内联样式)Inline style

http://www.cnblogs.com/xugang/archive/2010/09/24/1833760.html

  1. 内联样式表的权值最高 1000;

  2. ID 选择器的权值为 100

  3. Class 类选择器的权值为 10

  4. HTML 标签选择器的权值为 1

CSS 优先级法则:

A 选择器都有一个权值,权值越大越优先;

B 当权值相等时,后出现的样式表设置要优于先出现的样式表设置;

C 创作者的规则高于浏览者:即网页编写者设置的CSS 样式的优先权高于浏览器所设置的样式;

D 继承的CSS 样式不如后来指定的CSS 样式;

E 在同一组属性设置中标有“!important”规则的优先级最大;

@xgqfrms-GitHub
Copy link
Author

http://www.jianshu.com/p/a53ba8e1fe72
http://www.ruanyifeng.com/blog/2009/03/css_selectors.html
https://segmentfault.com/a/1190000005005091
https://segmentfault.com/a/1190000003860309

CSS 优先规则3:优先级关系:

内联样式 > ID 选择器 > 类选择器 = 属性选择器 = 伪类选择器 > 标签选择器 = 伪元素选择器

@xgqfrms-GitHub
Copy link
Author

选择器的权值不能进位

错误的说法

在学习过程中,你可能发现给选择器加权值的说法,即 ID 选择器权值为 100,类选择器权值为 10,标签选择器权值为 1,当一个选择器由多个 ID 选择器、类选择器或标签选择器组成时,则将所有权值相加,然后再比较权值。

这种说法其实是有问题的。

比如一个由 11 个类选择器组成的选择器和一个由 1 个 ID 选择器组成的选择器指向同一个标签,
按理说 110 > 100,应该应用前者的样式,然而事实是应用后者的样式。

错误的原因是:
选择器的权值不能进位。
还是拿刚刚的例子说明,11 个类选择器组成的选择器的总权值为 110,但因为 11 个均为类选择器,
所以其实总权值最多不能超过 100, 你可以理解为 99.99,所以最终应用后者样式。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment