Utils.js with latest Javascript ES6 syntax
Create the Utils.js file like this with multiple functions, etc
const someCommonValues = ['common', 'values'];
export const doSomethingWithInput = (theInput) => {
//Do something with the input
return theInput;
};
export const justAnAlert = () => {
alert('hello');
};
Then in your components that you want to use the util functions, import the specific functions that are needed. You don't have to import everything
import {doSomethingWithInput, justAnAlert} from './path/to/Utils.js'
And then use these functions within the component like this:
justAnAlert();
<p>{doSomethingWithInput('hello')}</p>
Answer from Fangming on Stack OverflowUtils.js with latest Javascript ES6 syntax
Create the Utils.js file like this with multiple functions, etc
const someCommonValues = ['common', 'values'];
export const doSomethingWithInput = (theInput) => {
//Do something with the input
return theInput;
};
export const justAnAlert = () => {
alert('hello');
};
Then in your components that you want to use the util functions, import the specific functions that are needed. You don't have to import everything
import {doSomethingWithInput, justAnAlert} from './path/to/Utils.js'
And then use these functions within the component like this:
justAnAlert();
<p>{doSomethingWithInput('hello')}</p>
If you use something like browserify then you can have an external file i.e util.js that exports some utility functions.
var doSomething = function(num) {
return num + 1;
}
exports.doSomething = doSomething;
Then require it as needed
var doSomething = require('./util.js').doSomething;
Moving/ share functions between components - React.js
The most challenging thing for me about React is sharing state variables between components.
React sharing method across components - javascript
React share all methods inside a function component that might interact with the state between another function component
Videos
I'm new to React/Typescript with a Java/Python background. Maybe I am misunderstanding how to do this (or perhaps its some sort of anti-pattern..) but is it really hard to share state variables between components? What if I have an inputbutton component that acts as a setter for a state variable and another component that relies on the output of setter?
It seems quite complicated too create a parent component and propagate the props through that...
Hooks are another way to go.
With custom hooks you can reuse your state dependent handlers with ease between components.
// hooks.js
const { useState } from 'react';
const useHandleChange = () => {
const [formValues, setFormValues] = useState({});
const handleChange = (e) => {
let { type, checked, name, value} = e.target;
value = type === 'checkbox' ? checked : value;
setFormValues({
[name]: value
});
};
return { formValues, handleChange };
}
// Component.js
import useHandleChange from './hooks';
const LoginForm = () => {
// you can use that on each component that needs the handleChange function
const { formValues, handleChange } = useHandleChange();
return (
// html template
);
};
You would need to convert your component to function component, though so I would propose this solution only if it doesn't require too much effort to refactor your code. Hooks does not work on class components.
Of course. What you are looking for is called Higher-Order Components
You can create a HOC with all the methods/logic you need to share across, and wrap the components you need to enhance with it.