CopyPastor

Detecting plagiarism made easy.

Score: 0.8380906277430393; Reported for: String similarity Open both answers

Possible Plagiarism

Plagiarized on 2021-01-13
by yahya parvar

Original Post

Original - Posted on 2019-04-17
by maktel



            
Present in both answers; Present only in the new answer; Present only in the old answer;

You can define a method in your parent component, pass it to child component and call it from props every time child's state changes, keeping the state in child component.
look at this example

const EnhancedTable = ({ parentCallback }) => { const [count, setCount] = useState(0); return ( <button onClick={() => { const newValue = count + 1; setCount(newValue); parentCallback(newValue); }}> Click me {count} </button> ) }; class PageComponent extends React.Component { callback = (count) => { // do something with value in parent component, like save to state } render() { return ( <div className="App"> <EnhancedTable parentCallback={this.callback} /> <h2>count 0</h2> (count should be updated from child) </div> ) } }
You can create a method in your parent component, pass it to child component and call it from props every time child's state changes, keeping the state in child component.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const EnhancedTable = ({ parentCallback }) => { const [count, setCount] = useState(0); return ( <button onClick={() => { const newValue = count + 1; setCount(newValue); parentCallback(newValue); }}> Click me {count} </button> ) };
class PageComponent extends React.Component { callback = (count) => { // do something with value in parent component, like save to state }
render() { return ( <div className="App"> <EnhancedTable parentCallback={this.callback} /> <h2>count 0</h2> (count should be updated from child) </div> ) } } <!-- end snippet -->


        
Present in both answers; Present only in the new answer; Present only in the old answer;