In this page we will navigate to a details of a store defined in the next steps.
- React JS is currently not supporting custom events. Other libraries such as Vue and Angular have already implemented that feature. Therefore we need to get a reference to our element and attach an event to it using the DOM API -
addEventListener
. We will add anitemPress
listener to the list of Smart Stores.
- First of all we need to create so called
ref
. More about refs could be read here. Basically a ref is a reference to the DOM of an element. We crate a ref by calling the following:
const Home = (props) => {
const storesListRef = React.createRef();
- We need to assign that ref in the
jsx
of our components. Lets add it to the following list:
- Attach a listener to the
itemPress
event of theui5-list
by callingaddEventListener
API.
const Home = (props) => {
const storesListRef = React.createRef();
useEffect(() => {
storesListRef.current.addEventListener("itemPress", event => {
alert("Item is pressed!");
});
}, []);
return (
- Press any of the
ui5-li
s of theui5-list
- Alert appears on the screen.
- Routing in our application is done using
react-router-dom
. We can change routes of our application by callingprops.history.push("__route__")
. Lets navigate to/stores/0
when any of the list items is pressed.
const Home = (props) => {
const storesListRef = React.createRef();
useEffect(() => {
storesListRef.current.addEventListener("itemPress", event => {
props.history.push("/stores/0");
});
}, []);
return (