Should I use useselector/useDispatch instead of mapStateToProps

Redux store state can be read and changed from anywhere in the component, including callbacks. Whenever the store state is changed the component rerenders. When the component rerenders, useSelector runs again, and gives you the updated data, later to be used wherever you want. Here is an example of that and a usage of useDispatch … Read more

React createContext issue in Typescript?

It appears defaultValue value for React.createContext is expected to be of type: interface IContextProps { state: IState; dispatch: ({type}:{type:string}) => void; } Once Context object is created for this type, for example like this: export const AdminStore = React.createContext({} as IContextProps); Provider React component should no longer complain about the error. Here is the list … Read more

React & TypeScript: Avoid context default value

There’s no way of avoiding declaring the interface and the runtime values, because TS’s types disappear at runtime, so you’re only left with the runtime values. You can’t generate one from the other. However if you know that you are only ever going to access the context within the TodoContextProvider component you can avoid initialising … Read more

“Error: No QueryClient set, use QueryClientProvider to set one”

As the error suggests, you need to wrap your application in a QueryClientProvider. This is on the first page of the docs: import { QueryClient, QueryClientProvider, useQuery } from ‘react-query’ const queryClient = new QueryClient() export default function App() { return ( <QueryClientProvider client={queryClient}> <Example /> </QueryClientProvider> ) }

Uncaught Error: Rendered fewer hooks than expected. This may be caused by an accidental early return statement in React Hooks

The problem is that in the first render, 3 useState hooks were invoked – name, age and license but after the age is changed to a value below 16, the useState for license is no longer invoked, resulting in only the first 2 hooks being invoked. As the React docs state: Don’t call Hooks inside … Read more

Handle an input with React hooks

How about writing a reusable function that returns the input value … and the <input> itself: function useInput({ type /*…*/ }) { const [value, setValue] = useState(“”); const input = <input value={value} onChange={e => setValue(e.target.value)} type={type} />; return [value, input]; } That can then be used as: const [username, userInput] = useInput({ type: “text” }); … Read more

Should I wrap every prop with useCallback or useMemo, when to use this hooks?

Not worth it, for multiple reasons: Even official docs say you should do it only when necessary. Keep in mind that premature optimization is the root of all evil 🙂 It makes DX (developer experience) far worse: it’s harder to read; harder to write; harder to refactor. When dealing with primitives (like in your example) … Read more

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