Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

change parent state from child use effect in react js

// wrap the callback with React.useCallback
const handleChange = useCallback(() => {
  setState(state + 1);
},[]);
Comment

update parent state from child react

class Parent extends React.Component {
    constructor(props) {
        super(props)

        // Bind the this context to the handler function
        this.handler = this.handler.bind(this);

        // Set some state
        this.state = {
            messageShown: false
        };
    }

    // This method will be sent to the child component
    handler(id) {
        this.setState({
            messageShown: true,
            id: id
        });
    }

    // Render the child component and set the action property with the handler as value
    render() {
        console.log(this.state);
        return (
           <div>
              <Child action={this.handler} />
              <div>{this.state.id}</div>
           </div>
        );
    }
}

class Child extends React.Component {
    render() {
        return (
            <div>
                {/* The button will execute the handler function set by the parent component */}
                <button onClick={() => this.props.action(1)} > button </button>
            </div>
        )
    }
}
ReactDOM.render(<Parent />, document.getElementById('main'));
Comment

PREVIOUS NEXT
Code Example
Javascript :: UnhandledPromiseRejectionWarning 
Javascript :: how to use brand icons in react 
Javascript :: check if jwt token is valid 
Javascript :: window frames javascript 
Javascript :: js function run one another 
Javascript :: javascript image preview before upload 
Javascript :: js rename property 
Javascript :: javascript weakset 
Javascript :: todo list javascript 
Javascript :: class 
Javascript :: big.js 
Javascript :: toast not showing 
Javascript :: dynamic classname react 
Javascript :: react native svg size 
Javascript :: javascript promise with ajax 
Javascript :: javascript eval alternative 
Javascript :: array indexof 
Javascript :: how to make chrome extension js 
Javascript :: datatable hide no data available in table 
Javascript :: express example 
Javascript :: Modify String with Uppercase 
Javascript :: leaflet js 
Javascript :: ArduinoJson.h 
Javascript :: jqvmap 
Javascript :: get last element from array javascript 
Javascript :: react native modal 
Javascript :: reactjs debounce 
Javascript :: vuejs jwt authentication example 
Javascript :: javascript developer 
Javascript :: how we can set react select required 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =