javascript - ES6 Import some functions as a object -
actions.js
export const setx = () => {...} export const sety = () => {...} export const sett = () => {...}
somecomponent.js
import {setx, sety, sett} 'actions' export class somecomponent extends react.component { constructor(props,context) { super(props) this.state={ x, y, t } } componentwillmount() { let reduxstate = this.store.getstate() object.keys(this.state).foreach(n => { let fn = n + '-changed'; this[fn] = evt => { let update = {}; update[n] = evt.target.value; this.setstate(update); retrievedfunction = ****//how retrieve imported actions setx,sety , sett name**** this.store.dispatch(retrievedfunction(evt.target.value)) } this.state[n] = reduxstate[n] }); }
will imported functions in global 'window'. not able find imported function access them name
allimportedfunction['set'+n ](evt.target.value) window['set'+n](evt.target.value)
or
is there way add imported function object
import {setx, sety, sett} actioncreators 'actions' actioncreators['set'+n ](evt.target.value)
import * actioncreators 'actions' -> works, dont want import functions
you can't that.
but can put them in object:
import {setx, sety, sett} 'actions' const actioncreators = {setx, sety, sett};
Comments
Post a Comment