简体   繁体   English

React/nextjs 在按钮单击时获取数据,但按钮位于不同的组件中

[英]React/nextjs fetch data on a button click, but the button is in a different component

I have a function in the that fetches the weather data for current location when button is clicked.我有一个功能,可以在单击按钮时获取当前位置的天气数据。 I want to get this data from the Location component to the pages/index.tsx where I have another components that displays that data.我想将此数据从 Location 组件获取到 pages/index.tsx ,其中我有另一个显示该数据的组件。

This is my component structure:这是我的组件结构:

App
  --Layout
    --Header
      --Location (here i fetch the data on click)
    --Home (state)
      --Display 1
        --SubDisplay 1
      --Display 2 

Do I have to pass the data through all the components to the index page?我是否必须将数据通过所有组件传递到索引页面? It would be:这将是:

  1. UP from Location to Header从位置向上到标题
  2. UP from Header to Layout从页眉向上到布局
  3. DOWN from Layout to Index page从布局向下到索引页面
  4. DOWN from Index page to another components DOWN 从索引页面到另一个组件

Is this the best way to handle this?这是处理这个问题的最好方法吗?

Due to React's nature regarding data flow, you can only pass props down from parent component to its children, this is known as one-way data binding or unidirectional data flow.由于 React 的数据流特性,您只能将props从父组件传递给其子组件,这称为单向数据绑定或单向数据流。 Based on the structure of your components, to achieve what you want to do you would need to use Context API or something like redux to centralize your data in one place (or just lifting the states up).根据组件的结构,要实现您想要做的事情,您需要使用Context API 或类似redux的东西将您的数据集中在一个地方(或者只是提升状态)。 However you can try this "workaround" so you don't need to modify your project's structure:但是,您可以尝试这种“解决方法”,这样您就不需要修改项目的结构:

Since your state is in the index/home page, you could define a function in this component that will receive as parameter the data fetched in Location component and set the state with this data, of course you'd need to pass this function via props to Layout component so it can pass it to Header and Header to Location (prop drilling), example:由于您的状态位于索引/主页中,因此您可以在此组件中定义一个函数,该函数将接收在Location组件中获取的数据作为参数并使用此数据设置状态,当然您需要通过props传递此函数Layout组件,以便它可以将其传递给HeaderHeader to Location (道具钻孔),例如:

const Home = () => {
  const [data, setData] = useState([]);

  const handleData = (data) => {
    setData(data);
  }

  return (
    <Layout handleData={handleData}>
      The rest of your components...
    </Layout>
  )
}

Then like I said, pass that function this way: Layout - Header - Location.然后就像我说的那样,以这种方式传递该函数:布局 - 标题 - 位置。

In Location :Location

const Location = ({ handleData }) => {
  const handleClick = () => {
    // Fetch your data on click event and pass it as parameter to handleData function

    handleData(fetchedData);
  }

  // The rest of your component...
}

Hope this helps you.希望这对您有所帮助。

Suggested solutions based on more complexity基于更复杂的建议解决方案

  1. Redux还原
  2. React context API反应上下文 API
  3. Lifting State Up提升状态

Lifting state up (Easier than other) sample code提升状态(比其他更容易)示例代码

  1. Define shipping function and weatherData as a state in Layout Component, then send shippingFunction into Header component then Location component as prop在 Layout 组件中定义 shipping function 和 weatherData 作为 state,然后将shippingFunction发送到 Header 组件,然后将 Location 组件作为 prop
const [weatherData,setWeatherData]=useState();
const shippingFunction = function(data) { setWeatherData(data);}
  1. Location Component位置组件

    • Call props.shippingFunction in Layout component when weather data was fetched, then send weather data into props.shippingFunction as parameter.获取天气数据时调用Layout组件中的props.shippingFunction ,然后将天气数据作为参数发送到props.shippingFunction Now you have weather data in Layout component现在您在 Layout 组件中有天气数据
  2. Layout Component布局组件

    • At this moment you have filled weatherData and you can pass it as prop into any component you want此时您已经填充了 weatherData ,您可以将其作为道具传递给您想要的任何组件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM