1

我遇到了一个问题,react-loadable导致我的一个输入组件在状态更新后重新渲染并失去焦点。我已经进行了一些挖掘,但找不到其他人有这个问题,所以我认为我在这里遗漏了一些东西。

我正在尝试react-loadable根据用户选择的主题将组件动态包含到我的应用程序中。这工作正常。

./组件/应用

import React from 'react';
import Loadable from 'react-loadable';

/**
 * Import Containers
 */
import AdminBar from '../../containers/AdminBar';
import AdminPanel from '../../components/AdminPanel';

import 'bootstrap/dist/css/bootstrap.css';
import './styles.css';

const App = ({ isAdmin, inEditMode, theme }) => {
    const MainContent = Loadable({
        loader: () => import('../../themes/' + theme.name + '/components/MainContent'),
        loading: () => (<div>Loading...</div>)
    });

    const Header = Loadable({
        loader: () => import('../../themes/' + theme.name + '/components/Header'),
        loading: () => (<div>Loading...</div>)
    });

    return (
        <div>
            {
                (isAdmin) ? <AdminBar
                                className='admin-bar'
                                inEditMode={inEditMode} /> : ''
            }
            <Header
                themeSettings={theme.settings.Header} />
            <div className='container-fluid'>
                <div className='row'>
                    {
                        (isAdmin && inEditMode) ? <AdminPanel
                                                    className='admin-panel'
                                                    theme={theme} /> : ''
                    }
                    <MainContent
                        inEditMode={inEditMode} />
                </div>
            </div>
        </div>
    );
};

export default App;

./components/AdminPanel

import React from 'react';
import Loadable from 'react-loadable';

import './styles.css';

const AdminPanel = ({ theme }) => {
    const ThemedSideBar = Loadable({
        loader: () => import('../../themes/' + theme.name +  '/components/SideBar'),
        loading: () => null
    });

    return (
        <div className='col-sm-3 col-md-2 sidebar'>
            <ThemedSideBar
                settings={theme.settings} />
        </div>
    );
};

export default AdminPanel;

这是我的<ThemedSideBar />组件的样子:

./themes/Default/components/SideBar

import React from 'react';

import ThemeSettingPanel from '../../../../components/ThemeSettingPanel';
import ThemeSetting from '../../../../containers/ThemeSetting';

import './styles.css';

const SideBar = ({ settings }) => {
    return (
        <ThemeSettingPanel
            name='Header'>
            <ThemeSetting
                name='Background Color'
                setting={settings.Header}
                type='text'
                parent='Header' />
            <ThemeSetting
                name='Height'
                setting={settings.Header}
                type='text'
                parent='Header' />
        </ThemeSettingPanel>
    );
};

export default SideBar;

./components/ThemeSettingPanel

import React from 'react';
import { PanelGroup, Panel } from 'react-bootstrap';

const ThemeSettingPanel = ({ name, children }) => {
    return (
        <PanelGroup accordion id='sidebar-accordion-panelGroup'>
            <Panel>
                <Panel.Heading>
                    <Panel.Title toggle>{name}</Panel.Title>
                </Panel.Heading>
                <Panel.Body collapsible>
                    {children}
                </Panel.Body>
            </Panel>
        </PanelGroup>
    );
};

export default ThemeSettingPanel;

./containers/ThemeSetting

import React, { Component } from 'react';
import { connect } from 'react-redux';

import { themeSettingChange } from '../App/actions';

import ThemeSetting from '../../components/ThemeSetting';

class ThemeSettingContainer extends Component {
    constructor(props) {
        super(props);

        this.handleOnChange = this.handleOnChange.bind(this);
    }

    handleOnChange(name, parent, value) {
        const payload = {
            name: name,
            parent,
            value: value
        };

        this.props.themeSettingChange(payload);
    }

    render() {
        return (
            <ThemeSetting
                name={this.props.name}
                setting={this.props.setting}
                parent={this.props.parent}
                type={this.props.type}
                handleOnChange={this.handleOnChange} />
        );
    }
}

//----Redux Mappings----//
const mapStateToProps = (state) => ({
});

const mapDispatchToProps = {
    themeSettingChange: (value) => themeSettingChange(value)
};

export default connect(mapStateToProps, mapDispatchToProps)(ThemeSettingContainer);

./component/ThemeSetting

import React from 'react';

import TextField from '../common/TextField';

import './styles.css';

const ThemeSetting = ({ name, setting, type, parent, handleOnChange }) => {
    return (
        <div className='row theme-setting'>
            <div className='col-xs-7'>
                {name}
            </div>
            <div className='col-xs-5'>
                {
                    generateField(type, setting, name, parent, handleOnChange)
                }
            </div>
        </div>
    );
};

function generateField(type, setting, name, parent, handleOnChange) {
    const value = setting ? setting[name] : '';

    switch (type) {
        case 'text':
            return <TextField
                        value={value}
                        name={name}
                        parent={parent}
                        handleOnChange={handleOnChange} />;
        default:
            break;
    }
}

export default ThemeSetting;

./components/common/TextField

import React from 'react';
import { FormControl } from 'react-bootstrap';

const TextField = ({ value, name, parent, handleOnChange }) => {
    return (
        <FormControl
            type='text'
            value={value}
            onChange={(e) => {
                handleOnChange(name, parent, e.target.value);
            }} />
    );
};

export default TextField;

当我的管理面板内的字段更新时,会触发状态更改。似乎这会触发react-loadable重新渲染我的<ThemedSideBar />组件,这会破坏我的输入并创建一个具有更新值的新组件。其他人遇到过这个问题吗?有没有办法停止react-loadable重新渲染?

编辑:是请求的回购链接。

4

1 回答 1

2

编辑:根据评论中的对话,我很抱歉,我误读了这个问题。此处的答案已更新(更新答案下方的原始答案)

更新的答案

react-loadable docs看来,Loadable HOC 似乎打算在方法之外调用render。在您的情况下,您正在ThemedSideBar加载AdminPanel. 我怀疑您TextEdit的输入中的更改,通过更新您的 Redux 状态,然后通过组件链传递回导致 React 考虑重新渲染AdminPanel。因为您的调用Loadable是在render方法内部(即AdminPanel是一个展示组件),react-loadable所以每次 React 访问该代码路径时都会展示一个全新的加载组件。因此,React 认为它需要销毁先前的组件以适当地使组件与新的 props 保持同步。

这有效:

import React from 'react';
import Loadable from 'react-loadable';

import './styles.css';

const ThemedSideBar = Loadable({
  loader: () => import('../../themes/Default/components/SideBar'),
  loading: () => null
});

const AdminPanel = ({ theme }) => {
    return (
        <div className='col-sm-3 col-md-2 sidebar'>
            <ThemedSideBar
                settings={theme.settings} />
        </div>
    );
};

export default AdminPanel;

原始答案

看来您的问题可能与您构建的方式有关,TextField而不是react-loadable.

以和处理程序FormControl为道具。这意味着您已经表明它是一个受控(而不是不受控)组件。value={value}onChange

如果您希望该字段在用户键入输入时采用更新的值,您需要传播处理程序捕获的更改onChange并确保它被反馈到value={value}prop 中的值。

现在,它看起来value总是等于theme.settings.Height或类似(大概是空/空)。

另一种方法是使 FormControl 成为不受控制的组件,但我猜你不想这样做。

于 2018-07-04T01:09:19.350 回答