DOM manipulation are the simplest stuff offered by a JS library. But in react everything dynamic has to be in the state. It becomes super diificult to just add or remove simple DOM nodes. Maybe this are the cons of React or probably I don't know any other way.
Take a look at this--
class Ripple extends React.Component {
state = {
ripples: []
}
render() {
const { className } = this.props;
return (
<div onClick={this.rippleNow} className={`${className} material__ripple`}>
{this.props.children}
{this.state.ripples.map((Item, index) => (
<Item key={index} />
))}
</div>
);
}
rippleNow = ({ currentTarget, clientX, clientY }) => {
const { x, y } = currentTarget.getBoundingClientRect(),
{ offsetHeight, offsetWidth } = currentTarget,
dia = Math.min(offsetHeight, offsetWidth, 100);
const styles = {
top: (clientX - x) - dia / 2,
left: (clientY - y) - dia / 2,
width: dia,
height: dia
}
const Wave = () => (
<div onAnimationEnd={this.removeRipple} style={styles}></div>
);
this.setState(prev => {
ripples: [...prev.ripples, Wave]
});
}
removeRipple = (e) => {
// How will i remove that element?
// Or is there any other way to do the same?
}
}
How will i remove the element from the state? Or is there any other way to add or remove DOM Elements in react . As react doesn't allow modifying DOM directly .
I am in a real problem . Thanks to those who'll even try to help .