简体   繁体   English

我该如何外包使用state的函数并执行setState

[英]How can i outsource functions that use state and do setState

I have a class which has many functions, i wish to outsource these functions and put each group of function inside a file of their own and then use them by importing and calling them. 我有一个包含许多功能的类,我希望将这些功能外包,并将每组功能放入各自的文件中,然后通过导入和调用它们来使用它们。
Usually this is very simple, you simply put the function bodies inside another file and then export them, but in my case i use this.state and this.setState , is it still possible to outsource these function? 通常,这非常简单,您只需将函数主体放在另一个文件中,然后将它们导出,但是在我的情况下,我使用this.statethis.setState还是可以外包这些函数的? if not, is there a better practice? 如果没有,是否有更好的做法?
Thank you. 谢谢。

I just knocked this up real quick with no testing whatsoever, tell me if this is kinda what you were looking for? 我只是在没有任何测试的情况下很快就把它敲了出来,请告诉我这是否是您想要的?

window._stateManager = new StateManager();

class X extends React.Component {
    constructor(props) {
        super(props);

        _stateManager.addStateFunction(this.setState.bind(this));
    }
}


class StateManager() {
    constructor() {
        this.stateFunctions = [];
    }

    addStateFunction(ss) {
        this.stateFunctions.push(ss);
    }

    updateStates(obj) {
        this.stateFunctions.forEach(ss => {
            ss((state => {
                // here you have access to "this.state" as state
                return Object.assign({}, state, obj);
                // whatever you return from this function changes the state of the react component
            }))
        })
    }
}

Why you want to update the state by the function that doesn't belong to your component or child component. 为什么要通过不属于您的组件或子组件的功能更新状态。 It doesn't make sense. 这没有道理。 You want to use redux there will be a single source of state of your entire App stored in the store. 您想使用redux,那么整个应用程序的状态源将存储在商店中。 All your state update logic will be separated in reducer and you would have access to action creator that can update your App's state. 您所有的状态更新逻辑都将在reducer中分离,并且您可以访问可更新应用程序状态的操作创建者。

暂无
暂无

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

相关问题 reactjs、setState、Interface props 和 Interface state 之间的关系相对于 typescript,如何使用 setState? - relation between reactjs ,setState, Interface props and Interface state with respect to typescript , how can I use setState? 在 React Context 中,如何在 state 函数中使用 state 变量? - In React Context, how can I use state variables in state functions? 如何使用 setState 更新 state.item[1]? - How can I update state.item[1] in state using setState? 如何正确使用 setState 更改 state 中的 object - How can I correctly use setState to change an object inside my state 如何避免未使用的 setState 函数? 可以在没有 setter 的情况下创建 React useState 吗? - How do I avoid unused setState functions? Can React useState be created without a setter? 如何解决不要直接改变状态。 将setState()与数组一起使用? - How to fix Do not mutate state directly. Use setState() with array? 如何外包由addEventListener函数产生的值? - How can I outsource a value that is the result of an addEventListener function? 使用 setState 时如何立即更新 state? - How do I get updated state immediately when using setState? 我有一个this.state,我需要通过setState传入我的componentDidMount,如何在setState中使用bind(this)? - I have one this.state and i need pass in my componentDidMount with setState, how use bind(this) in setState? setState():不直接改变状态。 使用setState() - setState(): Do not mutate state directly. Use setState()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM