如何在反应中将状态从一个类组件转移到另一个类组件?

发布于 2025-01-09 01:52:53 字数 2870 浏览 0 评论 0原文

我是 React 新手,我正在尝试制作一个 CV 应用程序。 我在 PersonalData.js 中有一个表单,当我点击“提交 btn”时,类的状态会更新值。 我想将此状态从 PersonalData.js 传递到 Header.js。在文档中,我找到了将其发送到主 App.js 但不发送到不同的类组件的方法。

这就是我的 App.js 的样子:

function App() {
  return (
    <div className="App">
      <Header />
    <main>
    <Router>
        <Navbar />
        <Routes>
          <Route path="/" exact element={<PersonalData/>} /> //i want the state from here to go to <Header />
          <Route path="/aboutMe" element={<AboutMe/>} />
          <Route path="/profesional" element={<Profesional/>} />
          <Route path="/education" element={<Education/>} />
          <Route path="/skills" element={<Skills/>} />
          <Route path="/attachments" element={<Attached/>} />
        </Routes>
      </Router>
    </main>
    </div>
  );
}

标头代码:

export default class Header extends Component {
  constructor(props) {
    super(props);

    this.state = {
      image: null,
    };
    this.onImageChange = this.onImageChange.bind(this);
    this.hiddenFileInput = React.createRef(null);
  }

  onImageChange = (event) => {
    if (event.target.files && event.target.files[0]) {
      let img = event.target.files[0];
      this.setState({
        image: URL.createObjectURL(img),
      });
    }
  };

  handleClick = (event) => {
    this.hiddenFileInput.current.click();
  };

  render() {
    return (
      <div className="headerGrid">
        <div className="addPhotoButton" onClick={this.handleClick}>
          <i className="fa-solid fa-circle-plus"></i>
          <input
            type="file"
            name="myImage"
            onChange={this.onImageChange}
            ref={this.hiddenFileInput}
            style={{ display: "none" }}
          />
        </div>
        <img className="headerElem" src={this.state.image} alt="" />

        <div className="headerElem">
          <h2>Full Name</h2>
          <p>Current ocupation</p>
          <div>
            <span>Location</span>
            <span>Age</span>
            <span>Sex</span>
          </div>
        </div>
        <div className="headerElem">
          <div>
            <i className="fa-solid fa-envelope"></i>
            <span>Email adress</span>
          </div>
          <div>
            <i className="fa-solid fa-phone"></i>
            <span>Telephone number</span>
          </div>
        </div>
      </div>
    );
  }
}

i am new to React and i am trying to make a CV app.
I have a form in PersonalData.js and when i hit submit btn , the state of the class updates the values.
I want to pass this state from PersonalData.js to Header.js. In the documentation i have find ways to send it to the main App.js but not to a different class component.

This is how my App.js looks like:

function App() {
  return (
    <div className="App">
      <Header />
    <main>
    <Router>
        <Navbar />
        <Routes>
          <Route path="/" exact element={<PersonalData/>} /> //i want the state from here to go to <Header />
          <Route path="/aboutMe" element={<AboutMe/>} />
          <Route path="/profesional" element={<Profesional/>} />
          <Route path="/education" element={<Education/>} />
          <Route path="/skills" element={<Skills/>} />
          <Route path="/attachments" element={<Attached/>} />
        </Routes>
      </Router>
    </main>
    </div>
  );
}

Header code:

export default class Header extends Component {
  constructor(props) {
    super(props);

    this.state = {
      image: null,
    };
    this.onImageChange = this.onImageChange.bind(this);
    this.hiddenFileInput = React.createRef(null);
  }

  onImageChange = (event) => {
    if (event.target.files && event.target.files[0]) {
      let img = event.target.files[0];
      this.setState({
        image: URL.createObjectURL(img),
      });
    }
  };

  handleClick = (event) => {
    this.hiddenFileInput.current.click();
  };

  render() {
    return (
      <div className="headerGrid">
        <div className="addPhotoButton" onClick={this.handleClick}>
          <i className="fa-solid fa-circle-plus"></i>
          <input
            type="file"
            name="myImage"
            onChange={this.onImageChange}
            ref={this.hiddenFileInput}
            style={{ display: "none" }}
          />
        </div>
        <img className="headerElem" src={this.state.image} alt="" />

        <div className="headerElem">
          <h2>Full Name</h2>
          <p>Current ocupation</p>
          <div>
            <span>Location</span>
            <span>Age</span>
            <span>Sex</span>
          </div>
        </div>
        <div className="headerElem">
          <div>
            <i className="fa-solid fa-envelope"></i>
            <span>Email adress</span>
          </div>
          <div>
            <i className="fa-solid fa-phone"></i>
            <span>Telephone number</span>
          </div>
        </div>
      </div>
    );
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

命硬 2025-01-16 01:52:53

您可以通过将状态提升到应用程序组件来共享。

  function App() {
      const [details,setDetails] = React.useState({
         fullName : "yourName"
         currenOcupation: "React Dev",
         location : "inVirtualDom",
         age:23,
          gender: "male",
          mail: "[email protected]",
          phone : 123456789,          })
      return (
        <div className="App">
          <Header details={details} />
        <main>
        <Router>
            <Navbar />
            <Routes>
              <Route path="/" exact element={<PersonalData details={details}/>} /> //i want the state from here to go to <Header />
              <Route path="/aboutMe" element={<AboutMe/>} />
              <Route path="/profesional" element={<Profesional/>} />
              <Route path="/education" element={<Education/>} />
              <Route path="/skills" element={<Skills/>} />
              <Route path="/attachments" element={<Attached/>} />
            </Routes>
          </Router>
        </main>
        </div>
      );
    }

You can Share by Lifting up the State to the App component.

  function App() {
      const [details,setDetails] = React.useState({
         fullName : "yourName"
         currenOcupation: "React Dev",
         location : "inVirtualDom",
         age:23,
          gender: "male",
          mail: "[email protected]",
          phone : 123456789,          })
      return (
        <div className="App">
          <Header details={details} />
        <main>
        <Router>
            <Navbar />
            <Routes>
              <Route path="/" exact element={<PersonalData details={details}/>} /> //i want the state from here to go to <Header />
              <Route path="/aboutMe" element={<AboutMe/>} />
              <Route path="/profesional" element={<Profesional/>} />
              <Route path="/education" element={<Education/>} />
              <Route path="/skills" element={<Skills/>} />
              <Route path="/attachments" element={<Attached/>} />
            </Routes>
          </Router>
        </main>
        </div>
      );
    }
煮酒 2025-01-16 01:52:53

你可以看看React文档中的Context:
https://reactjs.org/docs/context.html

You can take a look in Context in React Documentation:
https://reactjs.org/docs/context.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文