I'm trying to create a digital coloring page in React Native. My goal is when a user presses on individual SVG paths it changes their fill color.
The problem is that onPress event handlers cannot be applied to Paths specifically, so I had to add it to a TouchOpacity. I need to know from that onPress event which Path was selected. I added both a name and id to each Path, I cannot figure out how to extract those attributes from the click event.
const selectedColor = '#FF0000';
const [ color1, setColor1 ] = useState(WHITE);
const [ color2, setColor2 ] = useState(WHITE);
const [ color3, setColor3 ] = useState(WHITE);
const onPressSvg = (event) => {
const num = // <--- IDK?
switch (num) {
case 1: setColor1(selectedColor); break;
case 2: setColor2(selectedColor); break;
case 3: setColor3(selectedColor); break;
}
};
return (
<TouchableOpacity onPress={onPressSvg}>
<Svg viewBox="0 0 144 144">
<Path
id="1"
name="1"
fill={color1}
d="..."
/>
<Path
id="2"
name="2"
fill={color2}
d="..."
/>
<Path
id="3"
name="3"
fill={color3}
d="..."
/>
</Svg>
</TouchableOpacity>
);