简体   繁体   English

导出 React.JS 变量并在其他文件中使用

[英]Export React.JS variable and use in other file

So what I want to do is take the input from the radio buttons ( metric or imperial ) from Forecast.js , use them in a function in Conditions.js to determine whether it's hot or cold.所以我想做的是从Forecast.js的单选按钮(公制英制)中获取输入,在Conditions.js的 function 中使用它们来确定它是热的还是冷的。 I've exported the variable unit from Forecast.js but when I try to use it in the clothes() function it doesn't work.我已经从Forecast.js导出了变量单元,但是当我尝试在衣服() function 中使用它时它不起作用。 I've tried everything I found online and I tried to understand but I'm new to Reactjs.我已经尝试了我在网上找到的所有东西,我试图理解,但我是 Reactjs 的新手。

Conditions.js https://pastebin.com/RHbLuqZD条件.js https://pastebin.com/RHbLuqZD

import { unit } from '../Forecast/Forecast.js';

function clothes(t) {
    if (unit === "metric") {
        if (Number('t') <= 20)
            return (<>
                <img src={Cold} alt="wind icon" style={{ width: 50, marginRight: 20, height: 50 }} />
            It's cold, dress accordingly!
            </>);
        else
            return (<>
                <img src={Hot} alt="wind icon" style={{ width: 50, marginRight: 20, height: 50 }} />
            It's Warm, dress accordingly!
            </>);
    }

    if (unit === "imperial") {
        if (Number('t') <= 70)
            return (<>
                <img src={Cold} alt="wind icon" style={{ width: 50, marginRight: 20, height: 50 }} />
            It's cold, dress accordingly!
            </>);
        else
            return (<>
                <img src={Hot} alt="wind icon" style={{ width: 50, marginRight: 20, height: 50 }} />
            It's Warm, dress accordingly!
            </>);
    }
}

Forecast.js https://pastebin.com/wcBu8bwA Forecast.js https://pastebin.com/wcBu8bwA

export default Forecast;
export let unit = this.state.unit;

The reason it doesn't work could be because I didn't do the import and export properly or because I didn't use the imported variable properly.它不起作用的原因可能是因为我没有正确执行导入和导出,或者因为我没有正确使用导入的变量。

Edit: As I specified I'm a beginner, I don't know a lot, what I want is to see how to solve the problem and then understand how to do it.编辑:正如我指定的那样,我是初学者,我知道的不多,我想要的是看看如何解决问题,然后了解如何去做。 I just need to understand the problem with my code, or an alternative.我只需要了解我的代码或替代方法的问题。

  1. if('clothes' and 'Forecast' is a parent-child realationship) if('clothes' 和 'Forecast' 是父子关系)

    the 'unit' state, can be deliver by 'props' “单位”state,可以通过“道具”交付

function Parent(props){
const [unit,setUnit] = useState("")
return(
 <Child unit={unit}>
)
}
   
function Child(props){
  console.log(props.unit)
return(
<div/>
)
}

  1. else.别的。 you can learn about 'react-redux'你可以了解'react-redux'

This is not how the whole thing works.这不是整个事情的运作方式。 One state variable like unit in one component is not exportable like that.一个组件中的一个 state 变量类似unit不能这样导出。

There are mainly two things you can do:您可以做的主要有两件事:

  • one is pass this unit value to the Conditions.js as a prop and then pass it on to the clothes function (when using it, just like you pass the temperature value) to work with it;一种是将此unit值作为道具传递给Conditions.js ,然后将其传递给clothes function(使用时,就像您传递温度值一样)使用它;
  • another one is to have a state management tool like Redux , reactN or even use the React's built-in context .另一种是拥有 state 管理工具,例如ReduxreactN甚至使用React 的内置 context If you have need to use one of those only once it may not compensate the extra boilerplate, but it may be worth learning about them at least.如果您只需要使用其中之一,它可能无法补偿额外的样板,但至少值得学习它们。

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

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