Advertisement
ILyaCyclone

Untitled

May 9th, 2019
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. function MySelect(props) {
  4.     const [selectValue, setSelectValue] = React.useState(props.initialValue);
  5.  
  6.     // push browser history
  7.     React.useEffect(() => {
  8.         console.log("pushState: "+selectValue);
  9.         history.pushState({"a":selectValue, "b": 2}, "", "?a="+selectValue+"&b=2");
  10.     }, [selectValue]);
  11.  
  12.  
  13.     // subscribe to popstate
  14.     React.useEffect(() => {
  15.         const handlePopState = () => {
  16.             console.log("handlePopState, history.state.a: "+history.state.a);
  17.             setSelectValue(history.state.a);
  18.         }
  19.         window.addEventListener("popstate", handlePopState);
  20.         return () => {
  21.             window.removeEventListener("popstate", handlePopState);
  22.         }
  23.  
  24.     });
  25.  
  26.     function handleSelectChange(event) {
  27.         setSelectValue(event.target.value);
  28.     }
  29.  
  30.     return (
  31.         <React.Fragment>
  32.       <select value={selectValue} onChange={handleSelectChange}>
  33.         {
  34.             props.options.map((option) => (
  35.             <option key={option.value} value={option.value}>{option.display}</option>
  36.          ))
  37.         }
  38.       </select>
  39.       <br/>
  40.       select value is: {selectValue}
  41.         </React.Fragment>
  42.     );
  43. }
  44.  
  45.  
  46. const domContainer = document.querySelector('#MySelect');
  47. const options = window._selectOptions;
  48. const initialValue = window._selectInitialValue;
  49. ReactDOM.render(<MySelect options={options} initialValue={initialValue}/>, domContainer);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement