Con un componente React Class, tengo el siguiente objeto de estado como ejemplo:
state = { typeEvent: [], reductionCoefficient: [] }cuando quiero hacer una función que obtenga un atributo específico del estado, simplemente puedo usar
getArray = (name) => { return this.state[name] }¿Hay algo equivalente cuando uso Hooks? Porque tendría que escribir esto
const [typeEvent,setTypeEvent] = useState([]) const [reductionCoefficient,setCoefRed] = useState([]) const getArray = (name) => { if(name=='typeEvent'){ return typeEvent } if(name=='reductionCoefficient'){ return reductionCoefficient } }Simplemente puede escribir sus dos matrices en un estado:
const [state, setState] = useState({typeEvent:[], reductionCoefficient:[]});Ahora solo acceda a ellos por su nombre:
state.typeEvento
state.reductionCoefficient