Storing non-state variables in functional components

The useRef hook is not just for DOM refs, but can store any mutable value you like. Example function FunctionalBar(props) { const [foo] = useState(new Animated.Value(0)); const _foo = useRef(0); function showFoo() { let anim = Animated.timing(foo, { toValue: 1, duration: 1000, useNativeDriver: true }); anim.start(() => console.log(_foo.current)); } useEffect(() => { function _onChangeFoo({ value … Read more

Wrong React hooks behaviour with event listener

This is a common problem for functional components that use the useState hook. The same concerns are applicable to any callback functions where useState state is used, e.g. setTimeout or setInterval timer functions. Event handlers are treated differently in CardsProvider and Card components. handleCardClick and handleButtonClick used in the CardsProvider functional component are defined in … Read more

How can I use React hooks in React classic `class` component?

High order components are how we have been doing this type of thing until hooks came along. You can write a simple high order component wrapper for your hook. function withMyHook(Component) { return function WrappedComponent(props) { const myHookValue = useMyHook(); return <Component {…props} myHookValue={myHookValue} />; } } While this isn’t truly using a hook directly … Read more

How to change React-Hook-Form defaultValue with useEffect()?

@tam answer is halfway through what is needed to make it work with version 6.8.3. You need to provide the default value but also to useEffect to reset. That particular distinction is required if you have a form that you reload with another entity. I have a complete example in CodeSanbox here. In a nutshell: … Read more

React Function Components with hooks vs Class Components

The idea behind introducing Hooks and other features like React.memo and React.lazy is to help reduce the code that one has to write and also aggregate similar actions together. The docs mention few really good reason to make use of Hooks instead of classes It’s hard to reuse stateful logic between components Generally when you … Read more

How to test a react component that is dependent on useContext hook?

In general, using hooks shouldn’t change testing strategy much. The bigger issue here actually isn’t the hook, but the use of context, which complicates things a bit. There’s a number of ways to make this work, but only approach I’ve found that works with ‘react-test-renderer/shallow’ is to inject a mock hook: import ShallowRenderer from ‘react-test-renderer/shallow’; … Read more

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