Category: js

  • 用 shouldComponentUpdate 处理 reactjs 异步回传数据乱序的问题

    https://reactjs.org/docs/optimizing-performance.html

    class CounterButton extends React.Component {
      constructor(props) {
        super(props);
        this.state = {count: 1};
      }
    
      shouldComponentUpdate(nextProps, nextState) {
        if (this.props.color !== nextProps.color) {
          return true;
        }
        if (this.state.count !== nextState.count) {
          return true;
        }
        return false;
      }
    
      render() {
        return (
          <button
            color={this.props.color}
            onClick={() => this.setState(state => ({count: state.count + 1}))}>
            Count: {this.state.count}
          </button>
        );
      }
    }

    在这段 this.props 有时候仍会造成异步问题,改成 this.state 有时候可以修复:

    if (this.props.color !== nextProps.color) {
      return true;
    }
    if (this.state.count !== nextState.count) {
      return true;
    }
    return false;

  • node.js 无法访问 server.js

    在 node.js 的基础教程里面,一开始总是会教大家来写 server.js

    绝大多数情况下,这个例子会让大家觉得“yeah,写服务器好简单”。但是在某些 linux 虚拟主机下,这个简单的例子会无法实现。

    其实原因非常简单,防火墙过滤掉了 createServer 的 listen 端口。

    这时候只要把 listen 端口绑在常用的

  • 二次查询时 jQuery 失灵的问题

    一个透过 .prepend 或者 .text 做更新的页面中,有时候在检索第二次的时候 $(‘#fool’) 选择器 会提示:

    “TypeError: undefined is not a function”

    此问题通常是由于引用多个 js 库相互冲突导致的,解决的办法是把 $(‘#fool’) 换成 jQuery(‘#fool’) 。

  • bootstrap slider 拖动之后获得新值的办法

    $("#node").on('slideStop',function(data){
    alert(data.value);
    })