【问题标题】:React component self state (UI state)React 组件自身状态(UI 状态)
【发布时间】:2016-11-15 01:33:37
【问题描述】:

我正在使用 React-toolbox 对话框,我想将其包装为一个简单的确认对话框,它的工作方式类似于 alert() 显示带有消息和关闭按钮的对话框。

所以我有这样的演示组件

const ConfirmationDialog =({active, size, title,  message}) => {

  const onClickConfirm = ()=> {
    active = false;
  }

  return (
      <Dialog
        active={active}
        title={title}
        type={size}
      >
        <p>{message}</p>
          <button onClick={onClickConfirm}>Close</button>
      </Dialog>
  );
}

export default ConfirmationDialog;

active 来自一个道具,所以当我需要它时会显示确认对话框,但是我希望关闭按钮只是自行关闭对话框。

我真的必须传递handleOnClose 函数吗?每次我想使用这个组件时都必须传递那个函数似乎太多余了。 还是我真的必须创建一个容器类来实现这么简单的操作?

【问题讨论】:

    标签: reactjs redux


    【解决方案1】:

    您可以将您的ConfirmationDialog 组件作为类并使用本地状态来存储active 变量:

    import React, { Component, PropTypes } from 'react';
    
    class ConfirmationDialog extends Component {
      constructor(props) {
        super(props);
    
        // Setting `active` state property from props.
        this.state = {
          active: props.active,
        };
    
        // As we are passing this function as event handler, we need bind context,
        // to get access to `this` inside this function.
        this.handleCloseClick = this.handleCloseClick.bind(this);
      }
    
      componentWillReceiveProps(nextProps) {
        // When we will provide `active` variable via props, we will automatically set it to state.
        if (nextProps.active !== this.props.active) {
          this.setState({
            active: nextProps.active,
          });
        }
      }
    
      handleCloseClick() {
        // On clicking `close` button, setting `active` state variable to `false`,
        // it forces component to rerender with new state.
        this.setState({
          active: false,
        });
      }
    
      render() {
        const { title, size, message } = this.props;
        // Getting `active` variable from state, instead of props.
        const { active } = this.state;
        return (
          <Dialog
            active={active}
            title={title}
            type={size}
          >
            <p>{message}</p>
            <button onClick={this.handleCloseClick}>Close</button>
          </Dialog>
        );
      }
    };
    

    【讨论】:

    • 我无法让您的代码正常工作:S。当props.active 发生变化时,它不会重新渲染,所以即使我将活动道具更改为true,现在也不会渲染窗口:S
    【解决方案2】:

    您正在直接修改代码中的道具。我想知道这段代码是否有效。因为它不应该。

    您需要将active thingy 存储在组件状态的某处,要么是这个,要么(很可能)在上层组件中。

    而且看起来很简单,但使用 React 确实不是那么那么容易。查看this tutorial,这里我将解释如何使用模态弹出窗口执行things like that

    【讨论】:

    • 嗨,实际上它不起作用,因为道具无法更改。为什么活动状态应该在该组件的上层组件中?
    • 它也可能在 this 组件中,但是(假设我们正在讨论通用对话框包装器)在这种情况下,您将不会再次打开对话框被关闭。因为您不能直接从打开对话框的上层组件访问状态。因此,这种打开/关闭状态必须向上传播到实际决定何时必须打开此确认对话框的组件。
    • 嗯,事实上你可以。如果你从上层对这个对话框进行ref,你可以修改它的状态。可能,但推荐。您可以尝试这两种选择来感受它们的不同之处。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-30
    • 2016-12-17
    • 1970-01-01
    • 2018-12-25
    • 2021-01-25
    • 1970-01-01
    • 2016-10-31
    相关资源
    最近更新 更多