react-native - 从组件调用主文件中的函数
问题描述
我最近将我的应用程序从使用类组件重构为功能组件,并且在最后几件事上遇到了问题。
我的Home.js
样子如下(简化):
// imports....
import { StartStopButtons } from "../components/Button";
export default ({ navigation }) => {
const [scrollEnabled, setScrollEnabled] = useState(false);
const [elapsedMilliseconds, setElapsedMilliseconds] = useState(0);
const [isRunning, setIsRunning] = useState(false);
const [startTime, setStartTime] = useState(false);
const [stopTime, setStopTime] = useState(false);
const [isReset, setIsReset] = useState(true);
start = () => {
console.log("START");
// stuff
};
reset = () => {
console.log("RESET");
// stuff
};
stop = () => {
console.log("STOP");
// stuff
};
return (
<View style={styles.container}>
<StartStopButtons
isRunning={isRunning}
isReset={isReset}
elapsedMilliseconds={elapsedMilliseconds}
/>
</View>
);
};
MyStartStopButtons
有不同的外观,根据当前状态,它要么显示Start
,Stop
要么Reset
调用相应的函数。我目前正在将此情报放在另一个文件中,即我的Button.js
文件中。
Button.js
:
//imports....
export const StartStopButtons = ({
isRunning,
isReset,
elapsedMilliseconds,
}) => {
if (isRunning && isReset === false) {
return (
<View>
<TouchableOpacity onPress={stop}>
<Text>Stop</Text>
</TouchableOpacity>
<TouchableOpacity onPress={pause}>
<Text>Pause</Text>
</TouchableOpacity>
</View>
);
} else {
if (elapsedMilliseconds === 0) {
return (
<TouchableOpacity onPress={start}>
<Text>Start</Text>
</TouchableOpacity>
);
} else {
return (
<TouchableOpacity onPress={reset}>
<Text>Reset</Text>
</TouchableOpacity>
);
}
}
};
在重构之前,我使用,this.state.start
来this.state.stop
调用位于.start
stop
Home.js
我现在怎样才能做到这一点?有更好的方法吗?
解决方案
您可以将函数作为道具传递,就像传递isRunning
,isReset
和elapsedMilliseconds
.
但const
也请在函数名称之前添加。
// imports....
import { StartStopButtons } from "../components/Button";
export default ({ navigation }) => {
const [scrollEnabled, setScrollEnabled] = useState(false);
const [elapsedMilliseconds, setElapsedMilliseconds] = useState(0);
const [isRunning, setIsRunning] = useState(false);
const [startTime, setStartTime] = useState(false);
const [stopTime, setStopTime] = useState(false);
const [isReset, setIsReset] = useState(true);
const start = () => {
console.log("START");
// stuff
};
const reset = () => {
console.log("RESET");
// stuff
};
const stop = () => {
console.log("STOP");
// stuff
};
const pause = () => {};
return (
<View style={styles.container}>
<StartStopButtons
start={start}
stop={stop}
reset={reset}
pause={pause}
isRunning={isRunning}
isReset={isReset}
elapsedMilliseconds={elapsedMilliseconds}
/>
</View>
);
};
并像使用它们一样
//imports....
export const StartStopButtons = ({
start,
stop,
reset,
pause,
isRunning,
isReset,
elapsedMilliseconds,
}) => {
if (isRunning && isReset === false) {
return (
<View>
<TouchableOpacity onPress={stop}>
<Text>Stop</Text>
</TouchableOpacity>
<TouchableOpacity onPress={pause}>
<Text>Pause</Text>
</TouchableOpacity>
</View>
);
} else {
if (elapsedMilliseconds === 0) {
return (
<TouchableOpacity onPress={start}>
<Text>Start</Text>
</TouchableOpacity>
);
} else {
return (
<TouchableOpacity onPress={reset}>
<Text>Reset</Text>
</TouchableOpacity>
);
}
}
};
推荐阅读
- angularjs - AngularJS 和 Razor Kendo 事件
- mysql - 在 MySQL 5.7.22 中尝试使用 ENCRYPTION = 'Y'(密钥环插件)更改表时出现错误“解析 DDL 错误”
- html - 响应式网站标题部分的中心徽标
- cmake - CMake:未将-lrt连接到项目
- javascript - React Navigation 从自定义标题中的按钮导航到新屏幕
- excel - 在 Windows 和 Mac 上通过 VBA 宏发送带有工作簿的电子邮件
- javascript - 在 JS 中保存标签 (JSON) 的值
- javascript - Angular 6 使用带有插值的 ngFor 循环渲染 HTML 标签
- linux - 使用期望在远程机器上运行脚本
- android - 在 React-Native Expo 中的应用内购买?