【问题标题】:another case when the state of a component changes but seems that the component does not另一种情况是组件的状态发生变化但似乎组件没有
【发布时间】:2018-09-26 17:31:59
【问题描述】:

我有一个带有状态变量的反应组件:

showEditor

当 showEditor 为 false 时,它​​应该只显示一个带有一些数字的 div(最初 showEditor 为 false)。如果此状态变量为真,我的反应组件应该在另一个 div 内显示一个文本框和一个带有“保存”标签的按钮 - 使第一个带有数字的 div 消失。此文本框将用于更改数字。对于我定义的第一个 div(只显示数字的那个):

<div onClick={this.showEditorProc}>
    { this.state.showEditor ?
        <div ....>
            { just the numeric value }
        </div>
        :
        <div ....>
            textBox
            <button onClick={save and make show Editor false}>
                Save
            </button>
        </div>
    </div>

this.showEditorProc 函数会将 showEditor 变量的状态修改为 true,并且会出现保存按钮和文本框组件(也在另一个 div 中)。我创建了一个函数,如果单击保存按钮将执行该函数。此函数将 showEditor 变量修改为 false 但是,我看不到只有数值的 div。相反,我仍然看到带有保存按钮的文本框。还有什么我可能会丢失的吗?这是我的组件的代码:

import React from 'react';
import ReactDOM from 'react-dom';
import NumberFormat from 'react-number-format';

export class NumericBox extends React.Component {
    constructor() {
        super();

        this.state = {
            enteredValue: '',
            showNumEditor: false,
            index: ''
        };
        this.showNumericEditor = this.showNumericEditor.bind(this);
        this.handle_enteredValue_change = this.handle_enteredValue_change.bind(this);
        this.saveCellInfo = this.saveCellInfo.bind(this);
        this.loadBasicInformation = this.loadBasicInformation.bind(this);
    }


    saveCellInfo(e){
        alert(this.state.index);

        /*  cellAuxParams = new Map([['updateCellValue', this.state.updateCellValue]]); */

        console.log('numericBox.js>saveCellInfo>cellAuxParams= ------------------------------------------------ ' + 28);
        console.log(this.props.cellAuxParams);

        var f = this.props.cellAuxParams.get('updateCellValue');

        this.setState({showNumEditor: false}, () => f(this.state.Index, '77'));
    }

    handle_enteredValue_change(values) {
        const {formattedValue, value} = values;
        // formattedValue = $2,223
        // value ie, 2223

        this.setState({enteredValue: value});
    }

    showNumericEditor()
    {
        this.setState({showNumEditor: true})
    }


    loadBasicInformation()
    {
        this.setState({enteredValue: this.props.enteredValue,
            index: this.props.index
        });
    }

    componentDidMount(){
        this.loadBasicInformation();
    }

    componentWillReceiveProps(nextProps){
        alert(nextProps.enteredValue);
        this.setState({enteredValue: nextProps.enteredValue}, () => this.loadBasicInformation());
    }

    render() {
        const table4controls = {
            display: 'table',
            width: this.props.style.width,
            backgroundColor: 'white',
            border: '0px solid #666666',
            borderSpacing: '0px',
            paddingBottom: '0em',
            paddingTop: '0em'
        };

        const table4controls_RowStyle = {
            display: 'table-row',
            width: 'auto',
            clear: 'both',
            borderBottom: '5px'
        };

        const table4controls_ColsStyleA = {
            float: 'left',
            display: 'table-column',
            width: '60px',
            backgroundColor: 'white'
        };

        const table4controls_ColsStyleB = {
            float: 'left',
            display: 'table-column',
            width: '20px',
            backgroundColor: 'white'
        };

        const table4controls_ColsStyleC = {
            float: 'left',
            display: 'table-column',
            width: '20px',
            backgroundColor: 'white'
        };

        const btnStyle={

        };

        return (
            <div onClick={this.showNumericEditor}>
                { this.state.showNumEditor ?
                    <div style ={table4controls}>
                        <div style={table4controls_RowStyle}>
                            <div style={table4controls_ColsStyleA}>
                                <NumberFormat style={{width: '60px'}}
                                              value={this.state.enteredValue}
                                              thousandSeparator={true}
                                              prefix={this.props.prefix}
                                              onValueChange={this.handle_enteredValue_change}
                                />
                            </div>

                            <div style={table4controls_ColsStyleB}>
                                <button style={btnStyle} onClick={() => this.saveCellInfo(this.state.index)}>
                                    &#x25B2;
                                </button>
                                <button style={btnStyle} onClick={() => this.saveCellInfo(this.state.index)}>
                                    &#x25BC;
                                </button>
                            </div>

                            <div style={table4controls_ColsStyleC}>
                                <button style={btnStyle} onClick={(e) => {this.saveCellInfo(e, this.state.index)}}>
                                    Save
                                </button>
                            </div>
                        </div>
                    </div>
                    :
                    <div syle={table4controls_ColsStyleA}>
                        {this.state.enteredValue}
                    </div>
                }
            </div>
        );
    }
}

【问题讨论】:

  • 如果你能链接一些代码,或者通过codesandbox.io提供一个工作示例,那就太好了
  • 您需要提供整个组件。

标签: javascript reactjs jsx


【解决方案1】:

您在周围的div 上有一个onClick={this.showNumericEditor} 处理程序,因此当您按下保存按钮时,单击事件会冒泡并调用this.setState({showNumEditor: true})

要修复它,您可以重组渲染或在saveCellInfo 的开头调用e.stopPropagation();。另请注意,您的某些 this.saveCellInfo 调用未传递事件。

【讨论】:

  • 非常感谢......我发布这个问题后也得到了这个想法。很有趣的问题!
猜你喜欢
  • 2021-10-11
  • 1970-01-01
  • 2020-09-22
  • 2021-06-25
  • 2019-11-12
  • 2012-01-23
  • 2019-04-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多