小编JGP*_*ode的帖子

在react中导出功能组件内的函数

是否可以导出功能组件内部的函数并可以将其导入到另一个组件中?示例代码: 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)

export function reactjs

3
推荐指数
1
解决办法
2万
查看次数

标签 统计

export ×1

function ×1

reactjs ×1