React 生态圈

最近学了啥(一)

2017-08-03  本文已影响279人  梁相辉
compnentDidMount() {
 this._timeoutId = setTimeout( this.doFutureStuff, 1000 );
 this._intervalId = setInterval( this.doStuffRepeatedly, 5000 );
}
componentWillUnmount() {
 clearTimeout( this._timeoutId );
 clearInterval( this._intervalId );
}
// How to ensure that our animation loop ends on component unount
componentDidMount() {
  this.startLoop();
}

componentWillUnmount() {
  this.stopLoop();
}

startLoop() {
  if( !this._frameId ) {
    this._frameId = window.requestAnimationFrame( this.loop );
  }
}

loop() {
  // perform loop work here
  this.theoreticalComponentAnimationFunction()
  
  // Set up next iteration of the loop
  this.frameId = window.requestAnimationFrame( this.loop )
}

stopLoop() {
  window.cancelAnimationFrame( this._frameId );
  // Note: no need to worry if the loop has already been cancelled
  // cancelAnimationFrame() won't throw an error
}
@media screen and (max-width: 500px) and (min-resolution: 2dppx) {
    .yourLayout {
        width:100%;
    }
}
537f5932ly1ffrotx8adqj21kw162gxx.jpg ![a2.png](http:https://img.haomeiwen.com/i111568/40e1fbc6a94bc222.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
// 无效语法:
42.toFixed( 3 ); // SyntaxError
// 下面的语法都有效:
(42).toFixed( 3 ); // "42.000"
0.42.toFixed( 3 ); // "0.420"
42..toFixed( 3 ); // "42.000"
42 .toFixed(3); // "42.000"
var a = 1;
b = a;
b++
a // 1
b // 2

var arr = [1, 2, 3]
brr = arr
arr.push(4)
arr // [1, 2, 3, 4]
brr // [1, 2, 3, 4]
+ new Date()

更优雅的方式
(new Date()).getTime()

Date.now()
CallApi(`/message/${id}`, null, 'DELETE').then(r => {
  if (r.code === 'SUCCESS') {
    let index = this.state.messages.findIndex(i => i.id == id);
    // 快速复制一个新数组
    let newArr = [...this.state.messages];
    newArr.splice(index, 1);
    this.setState(s => ({messages: newArr}))
  } else {
    r.msg && Toast.info(r.msg)
  }
});
// 解构,如果需要改变变量名
let { message: newMessage } = this.state
function myNum(s) {
  var s1 = +s;
  if(!s1) return 0;
  var s2 = s1.toFixed(2) ;
  if(s2.substr(-1) !== '0'){
    return s2
  } else if(s2.substr(-2,1) !== '0' && s2.substr(-1) === '0'){
    return s1.toFixed(1)
  } else {return s1.toFixed(0)}
}
'font-size:20px'.replace(/:\s*(\d+)px/g, (a,b)=>{
               return `:${b*0.02}rem`
            })
import React from 'react';
import {render} from "react-dom";
import {Toast} from 'react-weui';

class ToastContainer extends React.PureComponent {
    state={show: true};
    componentDidMount() {
        const {timer, cb} = this.props;
        setTimeout(() => {this.setState({show: false}); cb && cb()}, timer*1000)
    }
    render() {
        const {type, content} = this.props;
        const {show} = this.state;
        return (
            show && <Toast icon={type} show={true}>{content}</Toast>
        )
    }
}

function hide() {
    render(<div />, document.getElementById('toast'))
}

function notice(content, timer, cb, type) {
    hide();
    const root = React.createElement(ToastContainer, {content, timer, cb, type});
    render(root, document.getElementById('toast'))
}


export default {
    success: (content, timer=3, cb) => notice(content, timer, cb, 'success-no-circle'),
    fail: (content, timer=3, cb) => notice(content, timer, cb, 'cancel'),
    info: (content, timer=3, cb) => notice(content, timer, cb, 'info-circle'),
    loading: (content, timer=3, cb) => notice(content, timer, cb, 'loading'),
    hide: () => hide()
};
  1. 首先为按钮添加样式 user-select:none
  2. 其次在按钮的 onTouchStart 事件上这样组织代码
onTouchStart = e => {
    e.preventDefault();
    setTimeout(() => { 你的核心代码 }, 0)
}
上一篇 下一篇

猜你喜欢

热点阅读