简体   繁体   English

如何清除React.js中的状态?

[英]How can I clear the state in React.js?

I think I may be missing something conceptually about how React and Reflux work. 我想我可能在概念上忽略了React和Reflux如何工作。

If I have set the state of an object ("project"), as it renders to the screen (with its existing properties), how can I then use that same state (and store) to create a new project? 如果我设置了对象(“项目”)的状态,当它呈现到屏幕(具有其现有属性)时,我如何使用相同的状态(和存储)来创建新项目?

This is some code from my project view: 这是我项目视图中的一些代码:

componentWillMount: function () {

    // We need to get the project based on projectID.
    var projectID = this.props.params.projectID;

    if (projectID) {
        ProjectActions.getProject(projectID);
    }
},

And here is some code from my project store: 这是我项目商店的一些代码:

data: {},

listenables: ProjectActions,

getInitialState: function() {
    return this.data;
},

onGetProject: function (projectID) {

    // Get the project based on projectID.
    $.ajax({
        type: 'GET',
        url: '/api/projects/getProject/' + projectID
    }).done(function(response){
        ProjectStore.getProjectSuccessful(response);
    }).error(function(error){
        alert('GET projects failed.');
    });

},

getProjectSuccessful: function(response) {
    for (var prop in response) {
        if (response.hasOwnProperty(prop)) {
            this.data[prop] = response[prop];
        }
    }
    this.trigger(this.data);
},

Then, say I click "new project", I use this code: 然后,说我单击“新项目”,我使用此代码:

mixins: [Reflux.connect(ProjectStore), Router.Navigation],

getInitialState: function () {
    return {
        // Project properties:
        format: '',
        title: '',
        productionCompany: ''

    };
},

What I've found is that if I remove the "getInitialState" from my store, that there is no issue when I go to create a new project. 我发现如果我从商店中删除“getInitialState”,那么当我去创建一个新项目时没有问题。 However, once I do that, I can no longer edit my existing project, because there is nothing attached to the state (I can use prop to view but that's not enough.) 但是,一旦我这样做,我就不能再编辑我现有的项目了,因为状态没有任何附加内容(我可以使用prop来查看,但这还不够。)

If I keep the "getInitialState" I get an error: 如果我保留“getInitialState”,我会收到错误:

Uncaught Error: Invariant Violation: mergeIntoWithNoDuplicateKeys(): Tried to merge two objects with the same key: `format`. This conflict may be due to a mixin; in particular, this may be caused by two getInitialState() or getDefaultProps() methods returning objects with clashing keys.

Do I need a "NewProjectStore" that only has a create action? 我需要一个只有创建动作的“NewProjectStore”吗? I had figured a store could handle create, get, update, etc. 我认为商店可以处理创建,获取,更新等。

Am I missing something? 我错过了什么吗?

When you use Reflux.connect , it adds getInitialState from the store into your component. 使用Reflux.connect ,它会将商店中的getInitialState添加到组件中。 That would be why you get the error about duplicate values. 这就是为什么你得到关于重复值的错误。 Your answer is either to use Reflux.listenTo or remove getInitialState from your component. 您的答案是使用Reflux.listenTo或从组件中删除getInitialState

Step 1: fix getInitialState() in your store 第1步:修复商店中的getInitialState()

There's nothing wrong with having one store to hold your edit project or new project, as long as you are ok with only having one or the other at any given time. 拥有一个商店来保存您的编辑项目或新项目没有任何问题,只要您可以在任何给定时间只拥有一个或另一个。 Based on your case, I would suggest keeping Reflux.connect and removing your component's getInitialState . 根据您的情况,我建议保留Reflux.connect并删除组件的getInitialState One problem is that getInitialState in your store doesn't have the default properties that are declared in the component. 一个问题是商店中的getInitialState没有在组件中声明的默认属性。

Step 2: fix your flux flow 第2步:修复助焊剂流量

This isn't wrong as in that it actually works, however, it's not following the flux guidelines. 这并没有 ,因为它确实有效,但是,它并没有遵循通量指南。 Most people agree that asynchronous actions belong in the action. 大多数人都认为异步操作属于该操作。 The store merely stores data and usually also provides helper functions for getting said data in different ways. 商店仅存储数据,并且通常还提供辅助功能以便以不同方式获得所述数据。 Try this: 试试这个:


Updated Code: 更新的代码:

var ProjectActions = Reflux.createActions({
    getProject: {asyncResult: true}
});
ProjectActions.getProject.listen(function (projectID) {
    // Get the project based on projectID.
    $.ajax({
        type: 'GET',
        url: '/api/projects/getProject/' + projectID
    }).done(function(response){
        ProjectActions.getProject.completed(response);
    }).error(function(error){
        alert('GET projects failed.');
        ProjectActions.getProject.failed(error);
    });
});

var ProjectStore = Reflux.createStore({
    init: function () {
        // default project properties:
        this.data = {
            format: '',
            title: '',
            productionCompany: ''
        };
    },

    listenables: ProjectActions,

    getInitialState: function() {
        return this.data;
    },

    onGetProjectCompleted: function(response) {
        for (var prop in response) {
            if (response.hasOwnProperty(prop)) {
                this.data[prop] = response[prop];
            }
        }
        this.trigger(this.data);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM