Answer the question
In order to leave comments, you need to log in
A few questions about React code.?
Please help me understand some things in the code that they do. I commented out the lines I didn't understand. Only 5 lines of code)
this.setState(
{
data, shift: Object.keys(data)[0] // (1)
}, this.filter // (2)
);
};
onClick = ({
target: { //(3)
dataset: { shift }
}
}) => {
this.setState(() => ({ shift }), this.filter); // (4)
};
return (
{days.map((day, i) => (
<button
key={day}
onClick={() => this.loadDay(i)} // (5)
className={i === this.state.day ? "active" : ""}
>
{day}
</button>
))}
);
}
}
Object.keys(data)[0]
data
setState
filter
onClick
target
filter
onClick
() => this.loadDay(i)
this.loadDay
Answer the question
In order to leave comments, you need to log in
1) Object.keys() -- returns an array of object keys. [0] -- selects the first element of this array
2) This is a callback, the method is called immediately after updating the state. Read the React documentation in the section about setState and its call arguments
3) Read about destructuring. This is the destructuring of the argument that comes into the function. In this case, the standard event object. Similarly:
onClick = event => {
const shift = event.target.dataset.shift;
...
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question