Skip to content

more than more useState,how to get the values #16219

@MaoGirlsQueen

Description

@MaoGirlsQueen

When using more than one useState, the response from the API contains more than one variable defined in the useState. How can we assign all states like this. setState ({... response}) to all states? If there are more than 10 useStates, this would be a waste of time.
for Example:

const [quickDate,setQuickDate] = useState( '9');
const [from,setFrom] = useState( getDateTime(getLastYear3Days()[0]));
const [to,setTo] = useState(getDateTime(getLastYear3Days()[1]));
const [index,setIndex] = useState('');
const [chargeState,setChargeState] = useState(0);
const [vin,setVin] = useState('');
const [vinStar,setVinStar] = useState('');
const [duration,setDuration] = useState('');
const [analysisType,setAnalysisType] = useState('single');
const [chartType,setChartType] = useState('bar');
const [selectColumns,setSelectColumns] = useState([]);
const [groupByColumns,setGroupByColumns] = useState([]);
const [rightBlock,setRightBlock] = useState([]);
const [dslFilters,setDslFilters] = useState([]);
const [dslFilterCombiner,setDslFilterCombiner] = useState('must');
const [dsl,setDsl] = useState('');
const [vinFilterVisible,setVinFilterVisible] = useState(false);
const [mySearch,setMySearch] = useState(null);

const mySearchInit = (searchId)=>{
dispatch({
type: 'common/fetchMySearchById',
payload: { id: searchId },
callback: (response) => {
let newState = JSON.parse(response.condition);
initVlaue(newState,response);
previewChart(newState);
mountInit(newState);
},
});
}
const initVlaue = (newState,response)=>{
setAnalysisType(newState.analysisType);
setChargeState(newState.chargeState);
setChartType(newState.chartType);
setDsl(newState.dsl);
setDslFilterCombiner(newState.dslFilterCombiner);
setDslFilters(newState.dslFilters);
setDuration(newState.duration);
setFrom(newState.from);
setGroupByColumns(newState.groupByColumns);
setIndex(newState.index);
setQuickDate(newState.quickDate);
setRightBlock(newState.rightBlock);
setSelectColumns(newState.selectColumns);
setTo(newState.to);
setVin(newState.vin);
setVinFilterVisible(newState.vinFilterVisible);
setVinStar(newState.vinStar);
setMySearch(response);
}

how to do ??

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions