score:16
2021 edit:
After implementation of React Hooks, the issue is no longer relevant - you can declare functions inside your functional component as long as you want. However if you want to avoid children re-render, you can wrap your functions with useCallback
hook so the function instance remains between renders.
const handleChange = useCallback(() => /* do stuff */, []);
https://reactjs.org/docs/hooks-reference.html#usecallback
Old answer:
I would suggest to keep your functions outside the Stateless Components as long as it's possible.
Consider following example. Your parent component re-renders, so the SFC child does (FYI: SFC re-renders everytime parent re-renders, it doesn't have any built-in shallow props comparison logic).
If you declare function inside SFC, it will create a completely new instance with every re-render.
If you declare function outside SFC, it will be used n-times, but the function itself will remain the same instance.
Working example at StackBlitz: https://stackblitz.com/edit/react-1m2hds
How to test it - write something inside input, then click on the child. Parent will re-render, so the child does. The outside-box function fn1
gets pushed to window variable test1
, the inside-box function fn2
goes into test2
. Now repeat process. If you compare functions inside test1
-> test1[0] === test1[1]
will return true
, since both of the functions are the same instance.
If you compare test2[0] === test2[1]
it will return false
, because new instance of function was created.
Note: Use built-in StackBlitz console for testing purposes.
Final note: regardless of what been told above, the perfomance differences are basically unnoticeable.
window.test1 = [];
window.test2 = [];
class App extends Component {
state = {
name: 'React'
};
handleChange = (e) => this.setState({ name: e.target.value });
render() {
return (
<div>
<Child name={this.state.name} />
</div>
);
}
}
const fn1 = () => {};
const Child = ({ name }) => {
const fn2 = () => {};
const checkout = () => {
window.test1.push(fn1);
window.test2.push(fn2);
}
return (
<div onClick={checkout}>{name}</div>
);
}
Source: stackoverflow.com
Related Query
- React - stateless component, function inside or outside component
- export function inside react component or access state in same file outside of component
- Invoke a function inside a React stateless component
- Difference between React useCallback with pure function declared inside and outside component
- How can I call a function that set sets an outside variable inside a custom component in React Native?
- calling function inside react functional component from outside
- React Hooks Error: Hooks can only be called inside the body of a function component
- React Warning: Cannot update a component from inside the function body of a different component
- Function inside functional component in React hooks - Performance
- Jest -- Mock a function called inside a React Component
- Testing functions inside stateless React component with Enzyme
- how to access vairables outside of map function in js and jsx in a React component
- Mock React useRef or a function inside a functional component with enzyme and jest?
- function inside stateless component
- React: How to add onChange functionality inside of Function component using React Hooks? Need onClick event from a checkbox to influence input state
- writing function outside of a class in react component
- React Native : Access Component state inside a static function
- React + Antd + Rollup Component Library "Error: Invalid hook call. Hooks can only be called inside of the body of a function component"
- React Hooks can only be called inside the body of a function component
- use of variable inside render function of react component
- React 17.0.1: Invalid hook call. Hooks can only be called inside of the body of a function component
- Receiving error - Hooks can only be called inside of the body of a function component when implementing React Spring basic example
- How to use React Context inside function of Class component
- React - Setting component state using a function outside of state, is it wrong?
- react - conditional rendering inside component return function
- is it ok to define a function inside a React stateless component?
- I got an error for using React.useRef inside a react function component
- React Hooks Mobx: invalid hook call. Hooks can only be called inside of the body of a function component
- React Hooks - function inside function component passed as prop can't access state
- How can we write a click handler for a Stateless Function Component in React JS
More Query from same tag
- Object doesn't support property or method 'entries' - error in IE11
- Implementing State Pattern Design in React (Select tool to draw in canvas)
- getting certain number of info to antd table
- Sublime text 3 - React & FlowType syntax support, white syntax after type definition
- React not updating the render after setState
- How to render react component onClick?
- dispatch() method in class-based components
- Is there a way to rearrange my state so that I can show the image URL?
- In React, is it possible to alter state inside of the map method, and if not what is an alternative?
- React Component is blank in the webpage
- React - get value of input on click
- React: why is getDefaultProps a method while propTypes is a plain object?
- material ui textfield wont be right aligned and right to left
- I can't map through the array 'products'(React/Redux)
- Why can't I check the checkbox?
- How do I reset the input field after clicking to send my message
- Attempted import error: 'default' is not exported from '../assertThisInitialized/_index.mjs'
- Evaluate component-scoped scss styles after global min.css in React
- How do I check if my event.target is typeof HTML input element in React?
- Add/remove state array item immutably
- Adding a record on a crud application
- Overlay text for OfficeUI fabric PivotItem
- passing variable from page to page react
- Searching for Webpack config for React Components Library with Typescript, SASS & CSS Modules via SASS support
- Reset the state of other component onClick using MUI in React
- Test the lifecycle methods of a connected component + ReactJs + Jest
- Video background doesn't show on page
- How to make Material UI modal and dialog scrollable?
- Converting flat array of object to array of nested objects
- how to use one more indeterminate checkbox column in react-table