react icon indicating copy to clipboard operation
react copied to clipboard

each child component render sync?

Open zllc opened this issue 9 years ago • 8 comments

i have wrote a tree component. a parent node, has 500 child node in a array. When the parent node update, each child node's shouldComponentUpdate compute sync. (each cost 2ms, total about 150ms). is there a way to compute shouldComponentUpdate asyc to avoid waste time?

zllc avatar Apr 26 '17 16:04 zllc

No. This still sounds a bit high to me.

What is your shouldComponentUpdate implementation like? Make sure it's just doing a shallow comparison. Deep comparisons can be worse than not having it at all.

Also make sure you are measuring the production build of React.

To help you further I would need to see (and run) your code.

gaearon avatar Apr 26 '17 23:04 gaearon

@gaearon

 class FileTreeNode extends Component {

      shouldComponentUpdate(nextProps, nextState) {
        //console.log(this.props.index)
        return !is(this.props.node, nextProps.node);
     }

     render() {
          const { node, fileType, detail, openNode, selectNode, onContextMenu, getDetail} = this.props

          const nodeDiv = (
                <div className={cx(styles.node, {[styles.focus]: node.get('isFocused')})}
                     ref={r => this.nodeDOM = r}
                     onDoubleClick={e => openNode(node)}
                     onClick={e => selectNode(node)}>
                    <span className={styles.nodeLabel}>{node.get('name')}</span>
                     <div className={styles.nodeBg}></div>
                  </div>
           )

          const children = []
          if (node.get('isDir')) {
              node.get('children').forEach((value, index)=>{
                 children.push(
                  <FileTreeNode
                        key={index}
                        index={index}
                        node={value}
                       fileType={fileType}
                       detail={detail}
                       onContextMenu={onContextMenu}
                       openNode={openNode}
                       selectNode={selectNode}
                       getDetail={getDetail}
                />
             )
             })
         }

         return (
               <div className={styles.nodeContainer} onContextMenu={e => { selectNode(node); onContextMenu(e, node) }}>
              {nodeDiv}
             {
                  node.get('isDir')
                    ?
                  <div className={cx(styles.children, {[styles.isFolded]: node.get('isFolded')})}>
                     {children}
                  </div>
                   :
                  null
             }
            </div>
          )
      }

      componentDidUpdate() {
            if (this.props.node.get('isFocused')) {
                  this.nodeDOM.scrollIntoViewIfNeeded && this.nodeDOM.scrollIntoViewIfNeeded()
            }
      }
    }
    export default FileTreeNode


    
     class FileTree extends Component {

            render() {
                 const {fileTree, ...actionProps} = this.props
                 const rootNode = fileTree.get('rootNode')
                 const context = fileTree.get('context')
                 const fileType = fileTree.get('fileType')
                 const detail = fileTree.get('detail')
                 const search = fileTree.get('search')[fileType]
               return (
                    <div className={styles.container}>

                     <div className={styles.fileTree} tabIndex={1} onKeyDown={this.onKeyDown}>
                     <FileTreeNode
                          node={rootNode}
                         index="-1"
                         fileType={fileType}
                         detail={detail}
                         onContextMenu={this.onContextMenu}
                         openNode={actionProps.openNode}
                         selectNode={actionProps.selectNode}
                        getDetail={actionProps.getDetail}
                    />
                </div>
              </div>
          )
       }
    }

zllc avatar Apr 27 '17 01:04 zllc

FileTree is the parent component, FileTreeNode is the child component. props node is like

  {
    id: '',
    isDir: '',
    children: [
         {
            id: '',
            isDir: '',
            children: [
            ]
         }
    ]
 }

node is immutable

zllc avatar Apr 27 '17 01:04 zllc

perf

zllc avatar Apr 27 '17 02:04 zllc

What does function is do? If your nodes are immutable, you should be able to compare them directly by reference:

shouldComponentUpdate(nextProps) {
  return this.props.node !== nextProps.node;
}

Hypnosphi avatar Apr 27 '17 13:04 Hypnosphi

Yea, it's not clear what is is.

gaearon avatar Apr 27 '17 13:04 gaearon

import { is } from 'immutable' is from immutable to compare by value, not by reference.

zllc avatar Apr 27 '17 14:04 zllc

We’ll need to have an example ready to run to see what’s up.

gaearon avatar Apr 27 '17 14:04 gaearon