決定一我的命運的不是能力而是選擇react
npm config set registry registry.npm.taobao.orgweb
File->Invalidate Cahes/Restart -> Invalidate and Restart
複製代碼
handlerClick = (questionId)=>{
console.log('我執行了')
}
<button type="button"
onClick={
this.handlerClick(question_id)
}>補習詳情
</button>
複製代碼
handlerClick (questionId){
console.log('我被點擊了')
}
<button type="button"
onClick={
this.handlerClick.bind(this,question_id)
}>補習詳情
</button>
複製代碼
function (){
console.log('我執行了')
}
複製代碼
(function (){
console.log('我執行了')
})()
複製代碼
<button type="button"
onClick={()=>{
this.handlerClick(question_id)
}
}>補習詳情
</button>
複製代碼
onClick=(function(){
console.log('我執行了')
})(questionId)
複製代碼
第二種等價於npm
onClick=function(){
return function handlerClick(questionId){
console.log('我執行了')
}
}
複製代碼
<Select defaultValue={0} onChange={this.handleClasses} className="selectClasses">
{
allClass.map((value, index) =>
<Select.Option value={index}>{value.class_name}</Select.Option>
)
}
</Select>
複製代碼
{
allClass.length !== 0 ?
<Select defaultValue={0} onChange={this.handleClasses} className="selectClasses">
{
allClass.map((value, index) =>
<Select.Option value={index}>{value.class_name}</Select.Option>
)
}
</Select> : <div/>
}
複製代碼
{
allClass.length !== 0 &&
<Select defaultValue={0} onChange={this.handleClasses} className="selectClasses">
{
allClass.map((value, index) =>
<Select.Option value={index}>{value.class_name}</Select.Option>
)
}
</Select>
}
複製代碼
this.state={
count = -1
};
this.setState({ count: 0 });
console.log(this.state.count);
複製代碼
this.setState({ count: 0 },
() => console.log(this.state.count))
複製代碼
export default class Component {
constructor(props = {}) {
this.props = props;
}
setState(state) {
const oldEl = this.el;
this.state = state;
this._renderDOM();
if (this.onStateChange) this.onStateChange(oldEl, this.el);
}
_renderDOM() {
this.el = this._createDOMFromString(this.render());
if (this.onClick) {
this.el.addEventListener('click', this.onClick.bind(this), false)
}
;
return this.el;
}
_createDOMFromString = (domString) => {
const div = document.createElement('div');
div.innerHTML = domString;
return div;
}
mount = (component, wrapper) => {
wrapper.appendChild(component._renderDOM());
component.onStateChange = (oldEl, newEl) => {
wrapper.insertBefore(newEl, oldEl);
wrapper.removeChild(oldEl);
}
}
onStateChange(oldEl, newEl) {}
render() {}
onClick() {}
}
複製代碼
this.props.history.push({
pathname: '/detail',
state: {
id: 3
}
)
複製代碼
防止重複跳轉死循環redux
this.props.history.replace('/detail');
複製代碼
goBack返回數組
this.props.history.goBack();
複製代碼
index.js註冊
import counter from './reducers/index';
import {createStore} from 'redux';
const store = createStore(counter);
const render = () =>
ReactDOM.render(<App
value={store.getState()}
onIncrement={() => store.dispatch({type: 'INCREMENT'})}//action
onDecrement={() => store.dispatch({type: 'DECREMENT'})}
/>, document.getElementById('root'));
render();
store.subscribe(render);
複製代碼
creare函數
export default (state = 0, action) => {
switch (action.type) {
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
return state
}
}
複製代碼
組件
const {value, onIncrement, onDecrement} = this.props;
<p>{value}次數拉拉拉</p>
<button onClick={onIncrement}>加上</button>
<button onClick={onDecrement}>減去</button>
複製代碼
0.07.toFixed()*100 = 7.00000000000000001%
複製代碼
(0.07*100).toFixed() = 7%
複製代碼
function length(str) {
return [...str].length;
}
複製代碼
或者bash
function countSymbols(string) {
return Array.from(string).length;
}
複製代碼
2.判斷一個字符是單字節仍是雙字節網絡
function is32(c){
return c.codePointAt(0) > 0xFFFFFF;
};
複製代碼
3.正則判斷金額是否正確react-router
let s = "1512.1";
let patter = /^\d+\.?\d{1,2}$/g.test(s);
複製代碼
4.將數組中布爾值爲false的成員轉爲0app
Array.from([1, , 2, , 3], (n) => n || 0)
複製代碼
5.數組去重dom
[...new Set([1,2,3,4,5,2])]
複製代碼
6.去除重複字符串
[...new Set('ababbc')].join('')
複製代碼