Code Monkey home page Code Monkey logo

reactcollect's People

Contributors

ckinmind avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

reactcollect's Issues

总结写了3个React页面后遇到的各种坑(别人的整理)

参考资料:总结写了3个React页面后遇到的各种坑

  • 标签里的ref属性的属性名不要出现中横杠比如message-content,如果有多个单词,直接写成驼峰形式(为什么?)
  • 如果组件之间的通信超过的一层,传参就非常麻烦,需要用到react的一个插件叫PubSub订阅发布
  • 利用ReactCSSTransitionGroup来添加一些简单的动画效果,比如列表内容增加或者删除时可以有渐进渐出的效果。但是这个标签默认是span标签,使用在table或者options里提示警告,不能在table和options里嵌套span,所以可以通过它本身的component属性,比如component="div"直接将这个标签渲染为我们需要的标签即可。另外css里对应的动画时间要和这里标签里的时间对应
  • React-Router和动画的结合使用:除了两种技术的结合之外,有个很重要的地方不要忘记,就是要让每个单独的组件设置样式为position:absolute,否则在动画切换时会出现短暂组件叠加的的情况,用户体验非常差,但是绝对定位后,每个组件都在同一个地方渐入渐出了

警告: Warning: It looks like you're using a minified copy of the development build of React...

执行webpack -p 打包压缩了js代码之后,在浏览器其运行时,提示了这个警告:

bundle.js:1 Warning: It looks like you're using a minified copy of the development build of React. When deploying React apps to production, make sure to use the production build which skips development warnings and is faster. See https://fb.me/react-minification for more details.e

警告的意思是:引入的React没有切换到产品版本

参考资料:webpack + react 优化:缩小js包体积

关于React的动画ReactCSSTransitionGroup(案例分析)

案例:在线地址:codepen.io

const App = ({ children, location }) => (
  <div className="app">
    <Navbar />
    <ReactCSSTransitionGroup
      component="div"
      transitionName="page"
      transitionEnterTimeout={700}
      transitionLeaveTimeout={700}
    >
      {React.cloneElement(children, {
        key: location.pathname
      })}
    </ReactCSSTransitionGroup>
  </div>
);

scss

.page-enter {
  transform: translateY(100%);
}

.page-enter.page-enter-active {
  transform: translateY(0%);
}

.page-leave {
  transform: translateY(0%);
  transition: 
    transform .7s cubic-bezier(
      0.680, 
      -0.550, 
      0.265, 
      3
  );
}

.page-leave.page-leave-active {
  transform: translateY(-100%);
}

几大问题:

  1. App这个组件是个函数式组件,里面的{ children, location }是什么意思
  2. ReactCSSTransitionGroup中各参数的意义
  3. 为什么要用React.cloneElement,和里面的key, 以及location.pathname

箭头函数,this

ES5写法:

//react 中的render函数
render(){

var a = [1,2,3];
a.forEach(function(){
     //在里面调用this.state, 需要在外面bind一下
}.bind(this));

return (
   <div>test</div>
);
}

ES6写法:

//react 中的render函数
render(){

var a = [1,2,3];
a.forEach(() => {
     //在里面调用this.state, 使用箭头函数,可以不用bind
});

return (
   <div>test</div>
);
}

关于ES6 super方法问题

react.js代码中经常出现class和super

class Todo extends Component{
    constructor(props){
        super(props)
    }
}

这里的super(props)什么作用?

答案:在子类的构造函数中,只有调用super之后,才可以使用this关键字,否则会报错。这是因为子类实例的构建,是基于对父类实例加工,只有super方法才能返回父类实例

参考资料:ES6 super方法问题
关联issue #15

如何指定图片的打包路径

在js里直接引入图片,然后最后打包的时候发现图片的路径不对,如何配置图片的打包路径

参考资料:webpack踩坑之路 (2)——图片的路径与打包

webpack.config.js

module.exports = {
  entry: './main.js',
  output: {
    path'dist',
    filename: 'bundle.js'
  },
  module: {
    loaders:[
      { test: /\.(png|jpg)$/, loader: 'url-loader?limit=8192&name=images/[name].[hash:8].[ext]'}
    ]
  }
};
  1. 在url-loader的最后添加name=images/[name].[hash:8].[ext], 它会将图片原名加上一个8位的hash码,然后存放到images文件夹下
  2. 注意output里面的path配置,我之前配置的是path:'dist/js' 这样打包后的bundle.js就会放到js目录下,但是这样做之后发现打包的图片文件夹images也被打包放到了js文件下,如果去掉path中的js,则bundle.js就无法放到js下,最后尝试是改变filename,变成filename:'js/bundle.js' ,再打包时nbundle.js就在js目录下了,imges文件夹也和js文件夹同级

关于import 和require

问题产生的原因,看到webpack.config.js里有require一个模块,然后又在其他文件看到impot 一个模块,突然搞不清楚这两种方式的由来和区别

// 比如在webpack.config.js中, 这样引入一个模块
var webpack = require('webpack');

//在别的js中又看到过这样引入模块
import React, { Component } from 'react';
import { Router, Route, Link, IndexRoute, browserHistory } from 'react-router';

疑问:关于route之间的嵌套是父子组件的嵌套吗

  1. 关于传统的父子组件,是指在一个组件里最后render的时候用了另外一个组件的标签,这时候这两个组件就构成了父子组件

  2. 路由route之间也可以嵌套,有父路由和子路由,在父路由对应的component中使用this.props.children就可以将子路由对应的组件包含进来,那么这时候构成父子组件吗?

  3. 怎么证明和测试?

警告:Warning: Each child in an array or iterator should have a unique "key" prop

出错代码:

 <div id="example"></div>
    <script type="text/babel">
      var arr = [
        <h1>Hello world!</h1>,
        <h2>React is awesome</h2>,
      ];
      ReactDOM.render(
        <div>{arr}</div>,
        document.getElementById('example')
      );
    </script>

错误信息:

react.js:19287 Warning: Each child in an array or iterator should have a unique "key" prop. Check the top-level render call using

参考资料1:Warning: Each child in an array or iterator should have a unique “key” prop
参考资料2:segmentfault

关于shouldComponentUpdate优化react的性能

参考资料:官方文档:shouldComponentUpdate

demo例子

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>
    );
  }
}
  1. 如果新的props.color和state.count和之前的一样的话就不更新
  2. 可以在render里加个console测试,如果状态没有变化,决定不更新,则不会执行render函数

关于React版本的问题(0.13/0.14的变化)

参考资料:关于React版本的问题

来自react画廊应用的问题去

主要的变化涉及以下几点:(1)

在React 0.14之前的版本, react component上的ref属性指向的是对React DOM Component的引用,并不是页面上真实的DOM节点,而在React 0.14之后(包括0.14) , ref开始指向真实的DOM节点

主要的变化涉及以下几点:(2)

在React 0.14之前的版本, 我们使用React Web相关的能力的时候, 只需要引入一个JS文件, react.js, 但React 0.14之后(包括0.14), react的设计团队为了将React的应用场景更加细化,同react-native, react-art, react-canvas, react-three 等形成并行, 将React Web的能力拆分成了 react.js, react-dom.js 并将DOM操作相关的接口,放在了react-dom.js的ReactDOM对象上,所以涉及一些API的变更:
React.render 变成了 ReactDOM.render
React.findDOMNode 变成了 ReactDOM.findDOMNode 等
至于React.render 这些在0.14.X的版本还能用,但是会有warning提示消息,0.15版本将会被彻底废弃,希望大家尽早规避使用老接口

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.