Quick actions

cmd+k|ctrl+k

Navigation

Languages

Working with hooks (Closure)

Snippet info

Language

JavaScript

Visibility

public

Author

paulkotov

Created

2019-02-11T19:35:07Z

Updated

2021-04-21T21:10:50.346382Z

function hook(param = null) {
    let data = param;
    
    let value = function(){
        return data;
    };
    
    let setValue = function(val){
        data = val;
    };
    
    // const newObj = new createDefaultObject(5);
    
    // const a = 5;
    // const a1 = x => console.log(x);
    // const arr = [];
    
    // arr.push(a);
    // arr.push(a1);
    
    // console.log(newObj.setValue(5));
    return [value, setValue];
}

let [value, setValue] = hook('initial');

console.log(value());
setValue('working');
console.log(value());

setValue('not working');
console.log(value());
INFO