首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何管理用户界面反馈?

如何管理用户界面反馈?
EN

Stack Overflow用户
提问于 2015-07-24 17:48:43
回答 3查看 1K关注 0票数 1

是否有一种已建立的模式用于管理用户与单个组件的交互,如显示加载器、在窗体保存/加载时禁用输入字段等?

我发现自己在商店中做了以下工作,以使组件与任何隐含状态保持一定程度的解耦:

代码语言:javascript
复制
function CampaignStore() {
    EventEmitter.call(this);

    AppDispatcher.register(payload => {
        switch (payload.type) {
            // [#1] ---------------v  (main action)
            case CampaignContants.SAVE:
                // [#2] ------------------------v  (prepare for the main action)
                this.emit(CampaignContants.WILL_SAVE);

                const data = payload.data;
                if (data.id) {
                    // [#3] ---v  (perform main action in store)
                    updateCampaign(payload.data).then(_ => {
                       // [#4] ------------------------v  (after main action)
                       this.emit(CampaignContants.DID_SAVE, 0)
                    });
                } else {
                    insertCampaign(payload.data).then(campaignId => this.emit(CampaignContants.DID_SAVE, campaignId));
                }
                break;
            // ...
        }
    }
}

基本上,我只需触发一个事件,说明某些操作即将发生,然后执行操作(make调用等),然后在操作完成时发出另一个事件。

在组件中,我只需订阅一个WILL_<action>事件,呈现所有的旋转器等等,然后在触发DID_<action>时清除屏幕。虽然这看起来很有效,但它确实让人感到非常乏味和重复,以及超级混乱(只存在太多的状态,只能根据操作的位置(在WILL_<action>和*DID_<action>之间)来调整UI。

代码语言:javascript
复制
// some component
var MyComponent = React.createClass({
   getInitialState: function () {
       return {
           items: [],
           loading: false,
           saving: false,
           checkingPasswordStrength: fase,
           // ...
       };
   },
   render: function(){
      return (
          <div>
             {this.state.loading && (
                 <p>Loading...</p>
             )}

             {!this.state.loading && (
                 // Display component in not-loading state
             )}
          </div>
      );
   }
});
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-07-24 21:25:28

我认为最好使用生命周期方法,如componentWillMountcomponentDidMountcomponentWillUpdatecomponentWillUnmount。使用这些方法,您可以检查前一个/current/next道具/状态(取决于方法)并对此作出响应。这样,您的存储只处理您的状态,您的组件变得更加纯净。

票数 1
EN

Stack Overflow用户

发布于 2015-10-08 21:02:44

我们在这里发现了一个简单的装载容器组件。所以就像这样:

代码语言:javascript
复制
const LoadingContainer = React.createClass({
    getDefaultProps: function() {
      return {isLoadedCheck:(res) => res.data!=null }
    },
    getInitialState: function() {
      return {isLoaded:false, errors:[]}
    },
    componentDidMount: function() {
      if(this.props.initialLoad) { this.props.initialLoad(); }
      if(this.props.changeListener) { this.props.changeListener(this.onChange); }
    },
    onChange: function() {
      let res = this.props.loadData();
      this.setState({errors: res.errors, isLoaded: this.props.isLoadedCheck(res)});
    },
    render: function() {
      if(!this.state.isLoaded) {
        let errors = this.state.errors && (<div>{this.state.errors.length} errors</div>)
        return (<div>{errors}<LoadingGraphic /> </div>)
      }
      return <div>{this.props.children}</div>
    }
  });

  const Wrapper = React.createClass({
    getDefaultProps: function() {
      return {id:23}
    },
    render: function() {
      let initialLoad = () => someActionCreator.getData(this.props.id);
      let loadData = () => someStore.getData(this.props.id);
      let changeListener = (fn) => someStore.onChange(fn);
      return (<div><LoadingContainer initialLoad={initialLoad}
        changeListener={changeListener}
        loadData={loadData}
        isLoadedCheck={(res) => res.someData != null}><SomeComponent id={this.props.id} /></LoadingContainer></div>)
    }
  });

虽然它添加了另一个无状态包装器,但它提供了一种干净的方法来确保组件不只是在挂载上加载,并且是显示api反馈等的公共地方,使用react 14,这些纯粹的无状态包装器得到了一些推动,perf也有了改进,因此我们发现它很好地扩展了。

票数 1
EN

Stack Overflow用户

发布于 2015-07-25 10:02:07

这是一种模式,它将帮助您使您的各个组件管理用户交互。

代码语言:javascript
复制
var MyComponent = React.createClass({
getInitialState: function() {
  return {
    item: [],
    loading: true,
  };
},
componentDidMount: function() {
  //Make your API calls here
  var self = this;
  $.ajax({
    method: 'GET',
    url: 'http://jsonplaceholder.typicode.com/posts/1',
    success: function(data) {
      if (self.isMounted()) {
        self.setState({
          item: data,
          loading: false
        });
      }
    }
  });
},
render: function() {
  var componentContent = null;
  if (this.state.loading) {
    componentContent = (<div className="loader"></div>);
  } else {
    componentContent = (
      <div>
        <h4>{this.state.item.title}</h4>
        <p>{this.state.item.body}</p>
      </div>
    );
  }
  return componentContent;
}});
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31616757

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档