reactjs - React:从单独的文件中返回函数结果
问题描述
我是 React 新手,目前正在通过构建一个项目来学习。
我有一个名为 Function.js 的文件,其中包含一个我导入到我的组件文件 (Component.js) 中的函数。
我正在尝试在 Function.js 中返回函数的值并将其插入到 div 中,但我不太确定如何解决这个问题......
函数.js
let testVariable;
export const getValue = (getValue) => {
switch (getIC) {
case "testing":
testVariable = "VALUE"
}
return testVariable
}
组件.js
import React, { useState } from "react";
import { getValue } from "./testfunc";
function StatusHubContent() {
const [getIC, getValueState] = useState("");
getValue(getIC)
return (
<div class="columns is-vcentered">
<div class="select">
<select
id="ic"
onChange={(e) => {
getValueState(e.target.value);
}}
>
<option selected disabled>
Person
</option>
<option value="Person A">Person A</option>
<option value="Person B">Person B</option>
</select>
</div>
<div>{getValue}</div>
</div>
);
}
我坚持如何返回值<div></div>
谢谢大家!
解决方案
你在做什么,也许我能帮上忙。无论如何,这就是我想出的
函数.js
let testVariable;
export const getValue = (value) => {
switch (value) {
case "testing":
testVariable = "VALUE"
}
return testVariable
}
组件.js
import React, { useState } from "react";
import { getValue } from "./testfunc";
function StatusHubContent() {
const [state, setState] = React.useState(null)
return (
<div class="columns is-vcentered">
<div class="select">
<select
id="ic"
onChange={(e) => setState(getValue(e.target.value))}
>
<option selected disabled>
Incident Commander
</option>
<option value="ayushl">Ayush Lal</option>
<option value="barriep">Barrie Pocock</option>
<option value="joeld">Joel Douglas</option>
<option value="nevashr">Nevash Reddy</option>
<option value="schalkb">Schalk Burger</option>
</select>
</div>
<div>{state}</div>
</div>
);
}
推荐阅读
- node.js - 有没有办法创建一个可用于多个文件的变量(Node.JS)
- c# - 如何为 .NET Core 3.0 Worker 服务设置事件日志
- excel - 如何在excel vba中选择特定范围?
- kubernetes - Kubernetes 故障排除教程
- python-3.x - tkinter 小部件:将参数传递给命令
- php - 以浏览器“打印到 PDF”的方式生成 PDF 文件
- excel - 根据 CustomerID 计算 3 个月平均值
- python - SelectConnection 没有连接到rabbitmq
- c++ - 发出读取文件并使用数组正确存储和检查内容
- php - 仅当数组存在且不为空时才将其包含到函数中