-->

  • How to get color output by passing the data into parent to child component in react js?

     

    How to get color output by passing the data into parent to child component in react js?



    Today in this blog, we will aware about get color output by passing data parent to child. So, without wasting any time lets do it but lets recap what is exactly React js




    React




    Certainly! ReactJS is a JavaScript library developed by Facebook for building user interfaces. It allows developers to create reusable UI components and efficiently manage the state of their applications. Here are some key aspects of ReactJS:


      
    Component-Based Architecture: ReactJS follows a component-based architecture, where the user interface is divided into small, reusable components. Components encapsulate their own logic, state, and rendering, making it easier to build and maintain complex user interfaces.

      
    Virtual DOM: ReactJS uses a virtual representation of the DOM (Document Object Model), known as the Virtual DOM. When the state of a component changes, React updates the Virtual DOM.

      
    JSX: JSX is a syntax extension for JavaScript used in React. It allows developers to write HTML-like code within JavaScript, making it easier to describe the structure and appearance of components. JSX code is transpiled to regular JavaScript using tools like Babel before being executed in the browser.
     

    Hooks: React introduced Hooks in version 16.8 as a way to use state and other React features in functional components. Hooks allow developers to write reusable logic and manage state within functional components without the need for class components. The most commonly used hooks are useState for managing state and useEffect for handling side effects such as fetching data or subscribing to events.

     


    React Router: React Router is a popular routing library for React applications. It enables developers to create single-page applications with multiple views and handles routing between different components based on the URL.

     


    State Management: React provides a flexible ecosystem of state management solutions. While React's built-in state management (useState ) is suitable for managing local component state, more complex applications may benefit from additional state management libraries like Redux. These libraries help manage global application state and provide predictable ways to update and access the state.



    ReactJS has gained widespread popularity due to its performance, reusability, and declarative approach to building user interfaces. It has large community. 




    NOTE - Here we will Tailwind CSS for designing.



    Here we first make a file as called UIcolor.jsx.



    import React, { useState } from 'react'
    import Callback from './Callback';
    
    function UIcolor() {
    
        const [UIcolor, setUIcolor] = useState(null);
         const getColor = (color) => {
            setUIcolor(color);
         };
    
    
      return (
        <div>
         <div 
         className=' grid grid-cols-1 '>
         <div className='border-2  p-12 w-56 mt-6 mx-6'
          style={{ background: `${UIcolor}`}}
         >
         </div>
         </div>
        
        <Callback getColor={getColor} />
    
        </div>
      )
    }
    
    export default UIcolor
    
    
    


    This is a React functional component called UIcolor that is responsible for rendering a color UI element. It uses the useState hook from React to manage the state of the color.


    Here's a breakdown of the component's structure and functionality:




    # The UIcolor component is imported along with the useState hook from the 'react' module.

    # The UIcolor component is defined as a function that returns JSX (React's syntax extension for JavaScript) to define the component's UI.

    # Inside the component, a state variable named UIcolor is created using the useState hook. It is initialized to null. The setUIcolor function is used to update the value of UIcolor.

    # The component defines a function named getColor which takes a color parameter. This function is responsible for updating the UIcolor state with the selected color.

    # The component's UI is defined using JSX. It consists of a <div> container that holds the color UI element.

    # The color UI element is represented by another <div> element with the CSS classes border-2, p-12, w-56, mt-6, and mx-6. These classes provide styling for the element, including a border, padding, width, margin, and positioning.

    # The style attribute is used to dynamically set the background color of the color UI element using the UIcolor state. The value of the UIcolor state is interpolated into the inline style using template literals (${UIcolor}).

    # The <Callback> component is rendered within the parent <div>. This component is assumed to be defined in a separate file and is responsible for providing a callback mechanism to select a color.

    # The getColor function is passed as a prop to the <Callback> component, allowing the selected color to be passed back to the UIcolor component and update the UIcolor state.

    # The UIcolor component is exported as the default export, which allows it to be imported and used in other parts of the application.




    and Now we will create another file called as Callback.jsx



    import React, { useState } from 'react'
    
    // function Callback () {
        const Callback = ({ getColor }) => {
        const [activecolor, setActivecolor] = useState();
        
        const handleChange = (e) => {
        const { value } = e.target;
        setActivecolor(value);
        getColor(value);
        };
    
      return (
        <div>
    
    
        <input
               type="text"
               id="input"
               aria-label='input'
               onChange={handleChange}
               value={activecolor}
               className='border-2 ml-6 pr-5 mt-5'
               
               />
        
        
        </div>
      )
    }
    
    export default Callback
    
    



    This is another React functional component called Callback that is responsible for rendering an input field and managing the selected color. It receives a prop named getColor which is a function provided by the parent component (UIcolor) to handle color selection.



    Here's a breakdown of the component's structure and functionality:



    # The Callback component is imported along with the useState hook from the 'react' module.

    # The Callback component is defined as an arrow function that receives the getColor prop as an argument. This prop is a function provided by the parent component (UIcolor) to handle color selection.

    # Inside the component, a state variable named activecolor is created using the useState hook. It is initialized without a value (undefined).

    # The component defines a function named handleChange which is triggered when the input field's value changes. It receives an event (e) parameter. The function extracts the value from the event target and updates the activecolor state with the new value. It also calls the getColor function provided by the parent component, passing the selected color as an argument.

    # The component's UI is defined using JSX. It consists of a <div> container that holds an <input> element.



    The <input> element has the following attributes:
            type="text": Specifies that it's a text input field.
            id="input": Sets the ID of the input field.
            aria-label='input': Provides an accessibility label for the input field.
            onChange={handleChange}: Binds the handleChange function to the input field's onChange event, so it is called whenever the value changes.
            value={activecolor}: Sets the value of the input field to the activecolor state, ensuring that it reflects the selected color and enables two-way data binding.
            className='border-2 ml-6 pr-5 mt-5': Applies CSS classes to style the input field, including a border, left margin, right padding, and top margin.
    




    # The component returns the JSX representing the input field within the enclosing <div>.


    And Now finally we will call UIcolor component in App.jsx.




    Output

















    Disclaimer



    All tutorials are for informational and educational purposes only and have been made using our own routers, servers, websites and other vulnerable free resources. we do not contain any illegal activity. We believe that ethical hacking, information security and cyber security should be familiar subjects to anyone using digital information and computers. Hacking Truth is against misuse of the information and we strongly suggest against it. Please regard the word hacking as ethical hacking or penetration testing every time this word is used. We do not promote, encourage, support or excite any illegal activity or hacking. 



  • 0 comments:

    Post a Comment

    For Any Tech Updates, Hacking News, Internet, Computer, Technology and related to IT Field Articles Follow Our Blog.