是否可以导出功能组件内部的函数并可以将其导入到另一个组件中?示例代码: https: //codesandbox.io/s/blissful-sanne-chk5g ?file=/src/App.js:0-275
第一部分:
import React from 'react'
const componentOne = () => {
function hello(){
console.log("Hello, says the componentOne")
}
return (
<div>
</div>
)
}
export default componentOne
Run Code Online (Sandbox Code Playgroud)
第二部分:
import React from 'react'
import {hello} from "./ComponentOne"
const componentTwo = () => {
return (
<div>
<button
onClick={hello}>
Hello
</button>
</div>
)
}
export default componentTwo
Run Code Online (Sandbox Code Playgroud)
应用程序.js
import ComponentTwo from "./components/ComponentTwo";
import "./styles.css";
export default function App() {
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to …Run Code Online (Sandbox Code Playgroud)