1

I have two files Sidebar and UserDataElements.

I want to display data of UserDataElements into Sidebar

I have tried this

This is the main file where i am fetching both the files.

 <Sidebar>
    <UserDataElements></UserDataElements>
  </Sidebar>

Sidebar.js

import React from "react"; 
import SidebarElements from "./SidebarElements";
 const Sidebar = () => {   return (
        <div className="sideBar">
          <SidebarElements></SidebarElements>
        </div>   ); };

export default Sidebar;

UserDataElements.js

import React from "react";
import userData from "./userData";
const UserDataElements = () => {
  return (
    <div>
      UserData
      <ul className="user-ul">
        {userData.map((val, key) => {
          return (
            <li
              key={key}
              onClick={() => {
                window.location.pathname = val.link;
              }}
              className="userDataList"
            >
              <div className="d-flex  ">
                <div id="name">{val.name}</div>
                <div id="branch">{val.branch}</div>
              </div>
            </li>
          );
        })}
      </ul>
    </div>
  );
};

export default UserDataElements;
2
  • 1
    You need to pass the data as props. Please have a look at the React tutorial which will cover this in detail, actually in more or less the first paragraph, that's how essential it is. Commented Apr 3, 2022 at 17:00
  • You use props for that, from which component to which component you want to pass the data? Commented Apr 3, 2022 at 17:01

2 Answers 2

1

You use props for that, which is just like an attribute in HTML, for example if you want to pass data from parent to child you can do it like this

<Sidebar>
    <UserDataElements data1={"some_data"} data2={"another_data"}>

    </UserDataElements>
</Sidebar>

And in UserDataElements you can access it using props

const UserDataElements = ({ data1, data2 }) => {
    // Here data1 and data2 will contain the data you have sent from parent to child
    ....
}

Or let's say, you want to pass data from child to parent, perhaps on click or something, then you can do it like this

import React from "react";
import userData from "./userData";
const UserDataElements = ({ data1, data2, onItemClick }) => {
  return (
    <div>
      UserData
      <ul className="user-ul">
        {userData.map((val, key) => {
          return (
            <li
              key={key}
              onClick={() => onItemClick && onItemClick(val, key)}
              className="userDataList"
            >
              <div className="d-flex  ">
                <div id="name">{val.name}</div>
                <div id="branch">{val.branch}</div>
              </div>
            </li>
          );
        })}
      </ul>
    </div>
  );
};

export default UserDataElements;

Note this specific line

onClick={() => onItemClick && onItemClick(val, key)}

Here we are invoking parent callback method, but before that we check if it exist, and In parent component we can access it like

import React from "react"; 
import SidebarElements from "./SidebarElements";

const Sidebar = () => {
    return (
        <div className="sideBar">
            <SidebarElements
                onItemClick={(val, key) => {
                    // here you get access to data of clicked element from child to parent
                }}
            >

            </SidebarElements>
        </div>
    ); 
};

export default Sidebar;

You should read more about component and props https://reactjs.org/docs/components-and-props.html

Sign up to request clarification or add additional context in comments.

Comments

1

For next people who want to do this, it was my case, I found the solution to do this in the React docs. This is known as lifting state up (that you can find here).

For your case @Harsh Mittal, you can add this line inside Sidebar.js:

const [yourData, setYourData] = useState(defaultValue);
  • yourData = you get the current state when you call it.
  • setYourData = the setter.
  • defaultValue = initial value of yourData.

With this variables, you will be able to create a function which performs the behaviors you want.

In UserDataElements.js:

Add parameter which receives a function. Then place it in the onClick prop of your <li>.

This gives you good basis for doing what you want to do.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.