NodeJS 在React中从一个页面到另一个页面获取变量

8dtrkrch  于 2023-06-29  发布在  Node.js
关注(0)|答案(2)|浏览(113)

我是react的新手,我试图在另一个页面中使用一个变量。
下面是我目前使用的代码:
TilbudSide.js:

import React, { useState, useEffect } from 'react';
import StartSide  from './StartSide';

function TilbudSide () {
  const [selectedCustomer, setSelectedCustomer] = useState(null);
  const [selectedCustomerNumber, setSelectedCustomerNumber] = useState(null);

  

  let apiUrlCostumers = null;
  const selectedCompany = StartSide().selectedCompany; // Access selectedCompany from StartSide
  console.log('SELECTEDCOMPANY '+ selectedCompany );
  if (selectedCompany === 'NOA') {
    apiUrlCostumers = 'http://localhost:8000/customers';
  };

  console.log(apiUrlCostumers)

return (  
      <div className="tilbud-side">
          <h2>Lag tilbud</h2>
      </div>
);
  
export default TilbudSide;

StartSide.js

import { useState } from "react";
import { useNavigate } from "react-router-dom";
import logo from './assets/NA_logo.svg';
import './StartSide.css';

function StartSide() {
    const [selectedCompany, setSelectedCompany] = useState('');
    const navigate = useNavigate()

    const handleClickNOA = () => {
        setSelectedCompany('NOA'); 
        navigate('/tilbudside');
        // Perform any additional logic or actions upon clicking the icon
    };

    console.log(selectedCompany);

    return (
        
        <div onClick={handleClickNOA} style={{ cursor: 'pointer' }}>
            <img src={logo} className="NA-logo" alt="NA Logo" />
        </div>
    );
}

export default StartSide;

每次尝试运行TilbudSide.js时,apiUrlCostumers的控制台日志始终为null,selectedCompany始终为null。我不明白为什么selectedCostumer在StartSide.js handleClickNOA()中没有改变。请帮帮我

xv8emn3q

xv8emn3q1#

我的建议是稍微改变状态处理,因为我认为有更简单和更干净的方法。
您可以使用以下方法来处理应用中的状态:
1.存储-它存储全局状态。像zustand或redux这样的库可以帮助你实现这一点
1.上下文-稍微复杂一点,但本地。为什么商店更好?因为你可以很容易地使用上下文创建非常占用内存的应用程序,并且如果使用多个子级更改状态,你还应该注意使用useMemouseCallback的重新渲染周期。仍然-让您在组件之间共享状态并深入传递它(了解props drilling

wydwbb8l

wydwbb8l2#

尝试从TilbudSide组件的StartSide检索selectedCompany变量是代码中的问题所在。现在,您正尝试将StartSide的状态作为组件直接访问。但是,您应该考虑创建一个不同的方法来返回所选的业务值。
TilbudSide.js:

import React, { useState, useEffect } from 'react';
import { StartSide } from './StartSide'; // Import the StartSide function instead of component

function TilbudSide() {
  const [selectedCustomer, setSelectedCustomer] = useState(null);
  const [selectedCustomerNumber, setSelectedCustomerNumber] = useState(null);

  const selectedCompany = StartSide().selectedCompany; // Call the StartSide function to get the selectedCompany value

  let apiUrlCustomers = null;
  if (selectedCompany === 'NOA') {
    apiUrlCustomers = 'http://localhost:8000/customers';
  }

  console.log('SELECTEDCOMPANY: ' + selectedCompany);
  console.log(apiUrlCustomers);

  return (
    <div className="tilbud-side">
      <h2>Lag tilbud</h2>
    </div>
  );
}

export default TilbudSide;

startside.js:

import { useState } from "react";
import { useNavigate } from "react-router-dom";
import logo from './assets/NA_logo.svg';
import './StartSide.css';

export function StartSide() { // Export StartSide as a function instead of component
  const [selectedCompany, setSelectedCompany] = useState('');
  const navigate = useNavigate();

  const handleClickNOA = () => {
    setSelectedCompany('NOA'); 
    navigate('/tilbudside');
    // Perform any additional logic or actions upon clicking the icon
  };

  console.log(selectedCompany);

  return (
    <div onClick={handleClickNOA} style={{ cursor: 'pointer' }}>
      <img src={logo} className="NA-logo" alt="NA Logo" />
    </div>
  );
}

StartSide导出为函数,调用该函数获取selectedCompany值,即可在TilbudSide组件中正确访问。

相关问题