Skip to main content

Posts

Showing posts with the label reactjs

React useState returns distinct state values per call

    The React doc states clearly that useState hook returns distinct values, but I still see some devs confused about whether they are working with a shared/global state, when useState is placed inside a custom hook   I put together a very simple codepen to illustrate that each call to the use hook function does indeed get you a distinct piece of state, which is nice! If you really want shared state, hello Redux (or ahem, Context) ! https://codepen.io/binodpanta/pen/vYWdxMd Have fun with React! ⌣

Enabling use of environment variables in ReactJS applications

I just wanted to share this small tidbit of information about a situation when you want to use environment variables in your ReactJS application. You can for example set a variable to TEST vs PRODUCTION to switch how your code operates (such as use a test db vs real db) First off, I am assuming you are using something like react-scripts which makes this a lot easier. If not you may have to first setup something like dotenv in your app But this just works if you are using react-scripts Create a .env file at the application root location (I think the REACT_APP prefix is needed for this to work) REACT_APP_SERVER=http://serverlocationDefault Now you can use this in your app like this render() {       return (     <Sessions server={process.env.REACT_APP_SERVER}/>       );     } Believe it or not , that's it! Now when you want to run with a different server all you have to run is run your app with th...