How do I reference a local image in React?

First of all wrap the src in {} Then if using Webpack; Instead of: <img src={“./logo.jpeg”} /> You may need to use require: <img src={require(‘./logo.jpeg’)} /> Another option would be to first import the image as such: import logo from ‘./logo.jpeg’; // with import or … const logo = require(‘./logo.jpeg); // with require then plug … Read more

React Router v4 – How to get current route?

In the 5.1 release of react-router there is a hook called useLocation, which returns the current location object. This might useful any time you need to know the current URL. import { useLocation } from ‘react-router-dom’ function HeaderView() { const location = useLocation(); console.log(location.pathname); return <span>Path : {location.pathname}</span> }

Typescript input onchange event.target.value

Generally event handlers should use e.currentTarget.value, e.g.: onChange = (e: React.FormEvent<HTMLInputElement>) => { const newValue = e.currentTarget.value; } You can read why it so here (Revert “Make SyntheticEvent.target generic, not SyntheticEvent.currentTarget.”). UPD: As mentioned by @roger-gusmao ChangeEvent more suitable for typing form events. onChange = (e: React.ChangeEvent<HTMLInputElement>)=> { const newValue = e.target.value; }

Programmatically navigate using react router V4

If you are targeting browser environments, you need to use react-router-dom package, instead of react-router. They are following the same approach as React did, in order to separate the core, (react) and the platform specific code, (react-dom, react-native ) with the subtle difference that you don’t need to install two separate packages, so the environment … Read more

axios post request to send form data

You can post axios data by using FormData() like: var bodyFormData = new FormData(); And then add the fields to the form you want to send: bodyFormData.append(‘userName’, ‘Fred’); If you are uploading images, you may want to use .append bodyFormData.append(‘image’, imageFile); And then you can use axios post method (You can amend it accordingly) axios({ … Read more

Why does calling react setState method not mutate the state immediately?

From React’s documentation: setState() does not immediately mutate this.state but creates a pending state transition. Accessing this.state after calling this method can potentially return the existing value. There is no guarantee of synchronous operation of calls to setState and calls may be batched for performance gains. If you want a function to be executed after … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)