ReactJS Feature: Split Screen that Can Be Moved

I am trying to create a screen with a split able and moveable feature, but I am facing difficulties in implementing it. I have tried using react split pane, but it is not working as expected. The functionality I am looking for is that when the user clicks on a profile icon in the lower part of the screen, the data in the upper part of the screen should change accordingly. Below is a sample code snippet that outlines the functionality I need. Feel free to implement it here and I will adjust it as needed.

https://i.sstatic.net/kWG6l.jpg

class MainComponent extends React.Component{
     constructor(){
       super();
       this.state={
               name:""
            }
       this.changeDetected=this.changeDetected.bind(this)
       }
        changeDetected(value){
                    this.setState({
                           name:value
                        })
              }

    render(){
       return(
          <Upper_compo data={this.state.name} />
          {*Splitting functionality here*}
          <Lower_compo changeDetected={this.changeDetected} />
         )
      }
}
export default MainComponent

//Upper part of Screen

class Upper_compo extends React.component{
   constructor(props){
      super();
      this.state={
          name:props.data
      }
    }

   render(){
       return(
             <>
                <h1>{this.state.name}</h1>
             </>
        )
    }
}
export default Upper_compo

//Lower part of Screen

class Lower_compo extends React.component{
   constructor(){
      super();
      this.state={
      }
    }

   render(){
       return(
             <>
                <input type="text" placeholder="name" onChange={this.props.changeDetected(value) />
             </>
        )
    }
}
export default Lower_compo

Answer №1

It appears that the issue lies within the Upper_compo component: you are saving the name in the state within the constructor, and then using this.state.Name in the render (which should actually be name with a lowercase n).

By doing this, you are disregarding the this.props.name, causing it to never change. Instead, you can simply use this.props.name inside the render of Upper_compo to ensure that the UI updates when the props name changes.

Furthermore, by keeping the state in the parent component, such as MainComponent, you are practicing the concept of Lifting State Up: https://reactjs.org/docs/lifting-state-up.html, which is a valid implementation.

Alternatively, you could also implement the getDerivedStateFromProps method within Upper_compo: https://reactjs.org/docs/react-component.html#static-getderivedstatefromprops. This allows you to update the state based on changes in the props, although I recommend sticking to the first approach I explained.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issues with Paypal react and Next JS buttons failing to reflect changes in shopping cart quantities

There seems to be an issue with the React PayPal Buttons integration. Despite updating the quantity of products in the cart, the changes are not being reflected in certain components like createOrder and onApprove within the PaypalButtons props. As a resul ...

What is the best way to send data from a Laravel Server to React using inertiajs?

I'm currently attempting to display a message after a post in React from a Laravel server using InertiaJS. After researching the documentation, online resources, and similar questions, I have come up with what I believe should work. I think the follo ...

Having trouble with UseSelector in React Redux?

I am currently working on a exercise to better understand the react-redux process, but I seem to have hit a roadblock. Any assistance would be greatly appreciated. One interesting observation is that when I subscribe and log the store into the console, it ...

"Optimize Magellan Sidebar for Mobile Devices by Relocating it to the Bottom of the Page

After spending a week working with Foundation 5 framework, I'm curious if there is a straightforward way to make my magellan sidebar shift to the bottom of the page when viewed on mobile or tablets. <div data-magellan-expedition="fixed"> <di ...

Navigating a collection of objects after a button is clicked

My current library project involves looping through an array of objects to display them on a grid based on input values. Here is the code snippet for my loop: const addBook = (ev) => { ev.preventDefault(); let myLibrary = []; let bookIn ...

The Challenges of CSS Specificity in Multiple Browsers

I am using this as an opportunity to learn, so I appreciate if you can refrain from telling me what not to do and instead help me understand the concepts. What I am trying to achieve here is comprehension. Here is a sample of CSS code: input[type="file"] ...

Next JS is successfully importing external scripts, however, it is failing to run them as

In my upcoming project using the latest version 12.1.6, I am incorporating bootstrap for enhanced design elements. The bootstrap CSS and JS files have been included from a CDN in the pages/_app.js file as shown below: import '../styles/globals.css&apo ...

Deleting child components by using shouldComponentUpdate

I currently have three main components in my project: SemesterList, Semester, and Course. Within the SemesterList component, there is a state that holds an array with all semesters and their corresponding content. In the Course component, I implement ...

Is there a way to prevent React's router private route from automatically redirecting to the login page when the page is refreshed?

App.js import './App.css'; import 'react-phone-number-input/style.css' import { useEffect } from 'react'; import { BrowserRouter as Router, Route, Routes } from 'react-router-dom'; import { useDispatch, useSelector } ...

Vue component always renders a new line

Whenever I include a <component> tag in my main application, it always renders on a new line. Is there a way to make it inline? I am trying to achieve something like this: <p>This is a component :<component></component></p> & ...

Is there a way to conceal the filter section in a Power BI report?

I am currently utilizing a asp.net 4.5 web application in conjunction with Power BI Embedded to display my developed reports on PowerBI. Thankfully, this integration is functioning smoothly. My current objective is to conceal the "Filters" section located ...

Error in React + Jest unit test caused by Material-UI typography issue

When I run my React web app in the browser, no warnings are displayed. However, when running unit tests, I am encountering a Material-UI typography error. Warning: Material-UI: you are using the deprecated typography variants that will be removed in the ...

emulate the removal of a material-ui Chip

I'm looking to create a test that removes a material-ui Chip component from my InputTag component. Does anyone have any suggestions on how to make this happen? Here is what I have tried so far: import React from 'react'; import InputTag fro ...

Is it possible to create this layout using Bootstrap 5? I want to stack spans, with one inside the container and one outside

Trying to achieve a specific design using Bootstrap 5.3. https://i.sstatic.net/STifm.jpg This design should be placed over the showcase, with the following code for the showcase displayed below: <div class="showcase d-flex flex-column"&g ...

Utilize the React.createContext() method integrated within the _app.js file

I need to retrieve the emailLogin function from my context in a different component: userContext.js import React, { useEffect, useState } from 'react'; import cookie from 'js-cookie'; import {firebase} from './firebase-client&apos ...

Leveraging NextJS to perform server side rendering by injecting parameters from a caller component

I'm currently in the process of creating an application with a storefront using nextJS. I've successfully utilized getServerSideProps when loading a new page. This particular page is quite complex, as it consists of multiple components, each req ...

Automatically setting the navbar to expand on medium devices

I am facing an issue while styling my navbar with BS5 installed. I have noticed that the className navbar-expand-md is being assigned, even though I have specified navbar-expand-custom in my code. navbar = _dbc.Navbar( [ _dbc.C ...

Is it possible for me to invoke a div within a different component?

I am facing a challenge with a large div component. <div id='download> ..... </div> My goal is to incorporate this same component into a Paper within a Modal. <Modal> <Box sx={style} > <Paper elevation ...

Troubleshooting: Issues with React Material-UI breakpoints not functioning

Trying to create a responsive navbar using Material-UI. The goal is to hide the IconButton between 960px and 1920px, and display it from 0px to 960px. However, it seems to work only below 960px. Here's a snippet of the code for IconButton and ul: ...

Can CSS be used to separate elements within a div from the overall page styles, similar to how an iFrame functions?

Is there a way to isolate elements within a div, similar to how it would behave in an iFrame? I am facing issues with the global SharePoint styles affecting my app inside SharePoint. I want to completely disable these global styles so that my app only use ...