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
You can't do that.
But you can put them in an object:
import {setX, setY, setT} from 'actions'
const actionCreators = {setX, setY, setT};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With