Git Product home page Git Product logo

Comments (4)

Souravkkt0404 avatar Souravkkt0404 commented on June 11, 2024

//class based
import React, { Component } from 'react';

class Counter extends Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
}

increment = () => {
this.setState({ count: this.state.count + 1 });
};

render() {
return (


Count: {this.state.count}


Increment

);
}
}

export default Counter;

//function based
import React, { useState } from 'react';

const Counter = () => {
const [count, setCount] = useState(0);

const increment = () => {
setCount(count + 1);
};

return (


Count: {count}


Increment

);
};

export default Counter;

from reactjs-interview-questions.

srd11 avatar srd11 commented on June 11, 2024

Using function based ,useState hook

import React, {useState} from 'react'
const Message = () => {
    const [message,setMessage] = useState('Welcome visitor');
    const changeMessage = () => {
        setMessage('Thank you for visting!');
    }

    return(
        <div>
            <h1>{message}</h1>
            <button onClick = {() => changeMessage()}>Subscribe</button>
        </div>
    )
}

Using class component same code.

import React, { Component } from 'react'

class Message extends Component{
    constructor() {
        super()
        this.state = {
            message: 'Welcome visitor'
        }
    }

    changeMessage() {
        this.setState({
            message: 'Thank you for visting! '
        })
    }

    render() {
        return (
        <div>
            <h1>{this.state.message}</h1>
            <button onClick = {() => this.changeMessage()}>Subscribe</button>
        </div>
        );
    }
}

;

from reactjs-interview-questions.

oppabgnamdz avatar oppabgnamdz commented on June 11, 2024

from reactjs-interview-questions.

sudheerj avatar sudheerj commented on June 11, 2024

@hamzaDev654 Thanks for the feedback. It is currently in progress to update them.

from reactjs-interview-questions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.