Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 Import some functions as a object

actions.js

export const setX = () => {...}
export const setY = () => {...}
export const setT = () => {...}

somecomponent.js

import {setX, setY, setT} from '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 to retrieve the imported actions setX,setY and setT by name****
        this.store.dispatch(RETRIEVEDFUNCTION(evt.target.value))
      }
      this.state[n] = reduxstate[n]
    });
}

Will all the imported functions be in the global 'window'. I was not able to find the imported function to access them by name

allimportedfunction['set'+n ](evt.target.value)
window['set'+n](evt.target.value)

or

is there way to add only the imported function into a object

import {setX, setY, setT} as actionCreators from 'actions'
actionCreators['set'+n ](evt.target.value)

import * as actionCreators from 'actions' -> This works, but I dont want to import all the functions

like image 485
Srikan Avatar asked Oct 15 '25 23:10

Srikan


1 Answers

You can't do that.

But you can put them in an object:

import {setX, setY, setT} from 'actions'
const actionCreators = {setX, setY, setT};
like image 81
SLaks Avatar answered Oct 17 '25 14:10

SLaks