I'm new to React / ES6 and this could be a basic issue. I'm creating a .js file that exports an array of json files. This data is not supposed to change, later when I click a "clear" button, I want to reset my data to the original data from this file. For some reason the data replacements I make to my state, are changing the original import. Any help would be much appreciated. I'm pretty sure I don't need Immutable.js in this scenario but i'm currently using it in hopes to remove this issue, so far it has not worked.
TimerData
const nelths = {
level: 10,
endTime: 1980,
objectives: [
{
id: 1,
objectiveName: "Rokmora",
objectiveCompleteTime: null
},
{
id: 2,
objectiveName: "Ularogg Cragshaper",
objectiveCompleteTime: null
},
{
id: 3,
objectiveName: "Naraxas",
objectiveCompleteTime: null
},
{
id: 4,
objectiveName: "Dargrul",
objectiveCompleteTime: null
},
],
}
const TimerData = [nelths];
export default TimerData;
Imports
import React, { Component } from 'react'
import TimerData from './timerData'
import FontAwesome from 'react-fontawesome'
import {Map} from 'immutable'
Constructor
constructor(props) {
super(props);
//const timerData = Object.assign({}, TimerData[0]);
var timerData = Map(TimerData[0]);
this.state = {
level: timerData.get('level'),
endTime: timerData.get('endTime'),
objectives: timerData.get('objectives'),
}
}
Modification
var objs = this.state.objectives.slice();
objs.forEach(function (element, index, array) {
element.objectiveCompleteTime = Date.now();
});
this.setState(Object.assign({}, this.state, {objectives: objs}));
Clear
var timerData = Map(TimerData[0]);
this.setState(Object.assign({}, this.state, {objectives: timerData.get('objectives'), active: false}));
I have cut out some of the code to try and save space and show the core components of my state init & modifications. Along with any hints about the issue and tips about bad practices would be appreciated.
Thank you for your time!