2017-05-03 92 views
0

我很努力地以簡明的方式提出這個問題。我的應用程序遇到一些主要的性能問題。我已經安裝了Perf附加工具以進行反應,並且可以查看問題出在哪裏,但是我不確定解決問題的最佳方法。React-Redux - 如何更新一段狀態而不會導致無關組件不必要的重新渲染

我認爲這可能與ReSelect有關......但需要一些指導從何處開始。我有一個呈現其他組件的組件。這使用size-me(計算瀏覽窗口的大小)和react-grid-layout(佈局每個組件並允許更改其定位)。這是資源密集型的,所以我不能不必要地發生這種情況。

用戶可以點擊一個按鈕來打開一個模式窗口(添加或編輯在網格中渲染的組件)。

問題:當模式窗口打開時,底層組件重新渲染,導致size-me和react-grid-layout重新渲染,從而導致模態「猛地」打開!

這是整個狀態樹: This is the entire state tree

這是改變狀態的只有一部分,當我打開模式: This is the only part of the state that changes when I open the modal

大小,我和應對電網布局的東西從狀態樹的formEngine.form部分渲染狀態,但是當對樹的formEngine.addComponent部分進行狀態更新時,它將被重新渲染

以下是性能日誌: Here are the performance logs

正如你所看到的,也有一些浪費呈現發生的事情,這隻會逐步增長基於用戶決定添加到表單嵌套佈局組件的數量...

所以嘗試防止這個問題變得太令人費解,讓我先問

  1. 如何防止重新渲染底層頁面當我打開模式?
  2. 爲什麼當fromEngine.addComponent被修改時,正在監視formEngine.form的組件被觸發以重新呈現?

謝謝。


編輯1:

我不知道這是否是相關的,但回答的評論,我加入這個代碼。 AddFormComponent是抽搐打開的模式。

Form.js:

const Form = (props) => (
    <div className="form-engine"> 
    <div className="card-block" style={{position: "relative"}}> 
     { 
     props.editMode && 
     <div className="nula-form-controls"> 
      <AddFormComponent parentId={"root"} /> 
     </div> 
     }    
     { 
     props.form.components.root.childComponentIds.length > 0 ? 
      <LayoutComponent componentKey={"root"} /> 
     : 
      <EmptyGridLayout /> 
     } 
    </div> 
    </div> 
) 

LayoutComponent。JS:

import React from 'react' 
 
import _ from 'lodash' 
 
import SizeMe from 'react-sizeme' 
 
import { Responsive as ResponsiveReactGridLayout } from 'react-grid-layout' 
 
import 'react-grid-layout/css/styles.css' 
 
import 'react-resizable/css/styles.css' 
 

 
import FormComponent from '../containers/FormComponent' 
 
import NestedLayoutComponent from '../containers/LayoutComponent' 
 

 
import AddFormComponent from '../containers/AddFormComponent' 
 
import LayoutComponentEditor from '../containers/LayoutComponentEditor' 
 

 
//Setup SizeMe Configuration 
 
let sizeMeConfig = { 
 
    monitorWidth: true 
 
} 
 
let sizeMeHOC = SizeMe(sizeMeConfig) 
 

 
//Wrap ResponsiveReactGridLayout in sizeMeHOC so that it is aware of it's width 
 
var GridLayout = ResponsiveReactGridLayout 
 
GridLayout = sizeMeHOC(GridLayout) 
 

 
const LayoutComponent = (props) => (
 
    <div> 
 
    <GridLayout 
 
     cols={props.cols} 
 
     className={props.className} 
 
     breakpoints={props.breakpoints} 
 
     rowHeight={props.rowHeight} 
 
     draggableCancel={props.draggableCancel} 
 
     layouts={props.layouts} 
 
     isDraggable={props.isDraggable} 
 
     isResizable={props.isResizable} 
 
     onLayoutChange={(currentLayout, allLayouts) => props.handleLayoutChange(props.componentKey, currentLayout, allLayouts)} 
 
     width={props.size.width} 
 
    > 
 
     { 
 
     //Map out any child layouts 
 
     props.childComponents.map((component) => { 
 
      if (component.type === "card") { 
 
      return (
 
       <div className={"card card-outline-" + component.color} key={component.key}> 
 
       <div className={"card-header card-" + component.color}> 
 
        {component.header} 
 
       </div> 
 
       <div className="card-block" style={{overflowY: "auto", position: "relative"}}> 
 
        { 
 
        //Hide if editMode={false} 
 
        props.editMode && 
 
         <div className="nula-card-controls"> 
 
         <LayoutComponentEditor path={component.key} /> 
 
         <a href="#" className="text-danger" title="Remove"><span className="fa fa-trash" /></a> 
 
         <AddFormComponent parentId={component.key} /> 
 
         </div> 
 
        }     
 
        <NestedLayoutComponent componentKey={component.key} /> 
 
       </div>    
 
       </div> 
 
      ) 
 
      } 
 
      else if (component.type === "fieldGroup") { 
 
      return (
 
       <div className="card" key={component.key}> 
 
       <div className="card-block pl-0 pr-0 pt-2 pb-0" style={{overflowY: "auto"}}> 
 
        { 
 
        //Hide if editMode={false} 
 
        props.editMode && 
 
         <div className="nula-fieldgroup-controls"> 
 
         <a className="text-warning" title="Edit"><span className="fa fa-pencil" /></a> 
 
         <a className="text-danger" title="Remove"><span className="fa fa-trash" /></a> 
 
         <AddFormComponent parentId={component.key} /> 
 
         </div> 
 
        }     
 
        <NestedLayoutComponent componentKey={component.key} /> 
 
       </div>    
 
       </div>     
 
      ) 
 
      } 
 
      else if (component.type === "paragraph") { 
 
      return (
 
       <div className="alert alert-success text-font-bold" key={component.key}> 
 
       { 
 
        <FormComponent component={component} editMode={props.editMode} /> 
 
       } 
 
       </div> 
 
      ) 
 
      } 
 
      else { 
 
      return (
 
       <div key={component.key}> 
 
       { 
 
        <FormComponent component={component} editMode={props.editMode} /> 
 
       } 
 
       </div>     
 
      ) 
 
      }   
 
     }) 
 
     } 
 
    </GridLayout> 
 
    </div> 
 
) 
 

 
export default SizeMe()(LayoutComponent)


編輯2:

AddFormComponent.js - 元器件

import React from 'react' 
 
import AddFormComponentDetails from './AddFormComponentDetails' 
 

 
import Perf from 'react-addons-perf'; // ES6 
 

 
class AddFormComponent extends React.Component { 
 

 
    constructor(props) { 
 
    super(props); 
 
    this.localOpenModal = this.localOpenModal.bind(this); 
 
    } 
 

 
    localOpenModal() { 
 
    console.log("----STARTING PERFORMANCE MONITOR-----") 
 
    Perf.start() 
 
    this.props.handleOpenModal(); 
 
    } 
 

 
    componentDidUpdate() { 
 
    console.log("-----PERFORMANCE MONITOR STOPPING------") 
 
    Perf.stop() 
 
    console.log("-----PRINT INCLUSIVE------") 
 
    Perf.printInclusive() 
 
    console.log("-----PRINT WASTEED------") 
 
    Perf.printWasted() 
 
    } 
 

 
    render() { 
 
    return (
 
    <span> 
 
    <a onTouchTap={this.localOpenModal} className="text-success" title="Add Component"> 
 
     <span className="fa fa-plus" /> 
 
    </a> 
 

 
    <Modal isOpen={this.props.modalOpen} size={"lgr"} toggle={this.props.handleCloseModal}> 
 
     <ModalHeader toggle={this.props.handleCloseModal}>Add Component</ModalHeader> 
 
     <ModalBody> 
 
     ...Removed For Breviety 
 
     </ModalBody> 
 
     <ModalFooter> 
 
     ...Removed For Breviety  
 
     </ModalFooter> 
 
    </Modal> 
 
    </span> 
 
) 
 
    } 
 
} 
 

 
export default AddFormComponent

AddFormComponent.js - 集裝箱

import { connect } from 'react-redux' 
 
import { 
 
    handleOpenModal, 
 
    handleCloseModal, 
 
    handleGoBack, 
 
    handleComponentPropertyChange, 
 
    handleComponentNameChange, 
 
    handleComponentTypeChange, 
 
    handleSubmit 
 
} from '../actions/addFormComponentActions' 
 
import AddFormComponent from '../components/AddFormComponent' 
 

 
const mapStateToProps = (state) => ({ 
 
    steps: [ 
 
    { icon: 'superpowers', title: 'Select Component', description: 'Select the Component you wish to add', active: state.addComponent.currentStep == 1 }, 
 
    { icon: 'info circle', title: 'Enter Details', description: 'Enter details to customize component', active: state.addComponent.currentStep == 2 }, 
 
    { icon: 'check', title: 'Add Component', description: 'Add component to form' } 
 
    ], 
 
    currentStep: state.addComponent.currentStep, 
 
    modalOpen: state.addComponent.modalOpen, 
 
    component: state.addComponent.component, 
 
    errors: state.addComponent.errors, 
 
    componentType: state.addComponent.componentType 
 
}) 
 

 
export default connect(
 
    mapStateToProps, 
 
    { 
 
    handleOpenModal, 
 
    handleCloseModal, 
 
    handleGoBack, 
 
    handleComponentPropertyChange, 
 
    handleComponentNameChange, 
 
    handleComponentTypeChange, 
 
    handleSubmit 
 
    } 
 
)(AddFormComponent)

addFormComponentReducer.js

import _ from 'lodash' 
 
import { 
 
    ADD_FORM_COMPONENT_TOGGLE_MODAL, 
 
    ADD_FORM_COMPONENT_CLOSE_MODAL, 
 
    ADD_FORM_COMPONENT_GO_BACK, 
 
    ADD_FORM_COMPONENT_SUBMIT, 
 
    ADD_FORM_COMPONENT_PROPERTY_CHANGE, 
 
    ADD_FORM_COMPONENT_PROPERTY_ERROR, 
 
    ADD_FORM_COMPONENT_KEY_ERROR, 
 
    ADD_FORM_COMPONENT_NAME_CHANGE, 
 
    ADD_FORM_COMPONENT_NAME_ERROR, 
 
    ADD_FORM_COMPONENT_TYPE_CHANGE, 
 
    ADD_FORM_COMPONENT_TYPE_ERROR 
 
} from '../actions/addFormComponentActions' 
 

 
let initialState = { 
 
    currentStep: 1, 
 
    modalOpen: false, 
 
    component: { 
 
    key: '', 
 
    label: '', 
 
    headingText: '', 
 
    text: '' 
 
    }, 
 
    errors: { 
 
    key: { 
 
     hasError: false, 
 
     msg: '' 
 
    }, 
 
    label: { 
 
     hasError: false, 
 
     msg: '' 
 
    }, 
 
    text: { 
 
     hasError: false, 
 
     msg: '' 
 
    } 
 
    } 
 
} 
 

 
function addFormComponentReducer(state = initialState, action) { 
 
    switch (action.type) { 
 
    case ADD_FORM_COMPONENT_TOGGLE_MODAL: 
 
     return { 
 
     ...state, 
 
     modalOpen: action.payload.isOpen, 
 
     currentStep: 1 
 
     } 
 
    case ADD_FORM_COMPONENT_CLOSE_MODAL: 
 
     return initialState; 
 
    case ADD_FORM_COMPONENT_GO_BACK: 
 
     return { 
 
     ...state, 
 
     currentStep: 1 
 
     } 
 
    case ADD_FORM_COMPONENT_SUBMIT: 
 
     return initialState; 
 
    case ADD_FORM_COMPONENT_PROPERTY_CHANGE: 
 
     return { 
 
     ...state, 
 
     component: { 
 
      ...state.component, 
 
      [action.payload.key]: action.payload.value 
 
     }   
 
     } 
 
    case ADD_FORM_COMPONENT_PROPERTY_ERROR: 
 
     return { 
 
     ...state, 
 
     errors: { 
 
      ...state.errors, 
 
      [action.payload.key]: { 
 
      hasError: action.payload.hasError, 
 
      msg: action.payload.msg 
 
      } 
 
     }   
 
     } 
 
    case ADD_FORM_COMPONENT_TYPE_CHANGE: 
 
     return { 
 
     ...state, 
 
     componentType: action.payload.componentType, 
 
     currentStep: 2 
 
     } 
 
    default: 
 
     return state 
 
    } 
 
} 
 

 
export default addFormComponentReducer

index.js - 結合熱度的CER

import { combineReducers } from 'redux' 
 

 
//import FormEngine reducers 
 
import formReducer from './formReducer' 
 
//import addFormComponentReducer from './addFormComponentReducer' 
 
import componentEditorReducer from './componentEditorReducer' 
 

 
const rootFormEngineReducer = combineReducers({ 
 
    form: formReducer, 
 
    //addComponent: addFormComponentReducer, 
 
    componentEditor: componentEditorReducer 
 
}) 
 

 
export default rootFormEngineReducer

rootReducer.js

import { combineReducers } from 'redux' 
 

 
//import reducers 
 
import rootCoreLayoutReducer from '../features/CoreLayout/reducers' 
 
import rootFormEngineReducer from '../features/FormEngine/reducers' 
 
import addComponentReducer from '../features/FormEngine/reducers/addFormComponentReducer' 
 

 
const rootReducer = combineReducers({ 
 
    coreLayout: rootCoreLayoutReducer, 
 
    formEngine: rootFormEngineReducer, 
 
    addComponent: addComponentReducer 
 
}) 
 

 
export default rootReducer

+0

什麼是組件的樹結構?模態組件是size-me和react-grid-layout的兄弟姐妹嗎? – rauliyohmc

+0

是的,我相信這是一個兄弟...不太清楚,如果這就是你要求的...但我添加了一些代碼。所以有AddFormComponent,它是打開的Modal。它顯示在頁面的頂部(在任何size-me/react-grid-layout內容發揮作用之前,但它也顯示在每個「LayoutComponent」中,因爲LayoutComponent可以包含嵌套的Components/LayoutComponents。 –

+0

你是否自由地使用'PureComponents'和/或'shouldComponentUpdate'?你是否只傳遞每個組件所需的道具(即沒有通用的狀態傳播)? – monners

回答

2

如果使用純組分的任何性能優化具有(使用shouldComponentUpdate)手動處理。既然你使用的是REDX,它可以爲你處理。但是你必須將它連接到redux商店。

如果您選擇使用Redux的連接保證了模態能見度不相關的其他屬性特別是在你的情況:

modalOpen嵌套在formEngine。當它改變任何聽到formEngine的東西時,就會退回

+0

謝謝你的回答。我測試了這一點。我移動了modalOpen在formEngine之外,但是我仍然在Perf日誌中獲得了相同的結果(它仍然重新渲染了SizeMe/react-grid-layout),我在上面添加了這個組件的代碼,這可能與SizeMe檢測有關寬度的變化?即使沒有向瀏覽器添加滾動條,它仍然會重新渲染,因此可能不會。 –

+0

一眼看來,您似乎正在通過「modalOpen」作爲道具。由於父組件負責傳遞此值,因此modalOpen中的更改將導致父級重新呈現,因爲它必須向下傳遞道具。無論如何,這是我的猜測。嘗試將你的模態直接連接到redux商店 –

+0

我還在傳遞連接的mapStateToProps方法中硬編碼屬性。我玩了一下,似乎也造成了問題。不完全知道爲什麼,因爲狀態沒有改變,但是當我從其中刪除道具並將它們作爲縮減器中的初始狀態時,佈局組件在打開模態時停止了重新渲染。 –

相關問題