javascript - Way to access const from another component in react - Stack Overflow

This is my first time to develop a react application.I am encountering difficulties in retrieving const

This is my first time to develop a react application.

I am encountering difficulties in retrieving const data from another ponent.

Here is the const I want to access to the other ponent.

TableComponent.js

export default function({ infinite }) {
const [checkedMap, setCheckedMap] = useState(new Map());

const viewDetails = () => {
    return (
      "url here" +
      Array.from(checkedMap.keys()).join() +
      "/details"
    );
  };
}

//other codes

Is there a way how to access viewDetails to a different ponent?

Thank you.

This is my first time to develop a react application.

I am encountering difficulties in retrieving const data from another ponent.

Here is the const I want to access to the other ponent.

TableComponent.js

export default function({ infinite }) {
const [checkedMap, setCheckedMap] = useState(new Map());

const viewDetails = () => {
    return (
      "url here" +
      Array.from(checkedMap.keys()).join() +
      "/details"
    );
  };
}

//other codes

Is there a way how to access viewDetails to a different ponent?

Thank you.

Share Improve this question edited May 24, 2019 at 10:00 Dan asked May 24, 2019 at 9:37 DanDan 6234 gold badges10 silver badges23 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 1

you have to return your const viewDetails...either you need to self invoking function or return the function...e.g.

export default function({ infinite }) {
const viewDetails = () => {
    return (
      "url here" +
      Array.from(checkedMap.keys()).join() +
      "/details"
    );
  };
return viewDetails;
}

If you export the const:

export const viewDetails = () => {
    return (
      "url here" +
      Array.from(checkedMap.keys()).join() +
      "/details"
    );
  };
}

This must be in the module scope (on the same level as the default export):

export const viewDetails = () => {}
export default function({ infinite }) {}

You can then import from the other ponent:

import { viewDetails } from './TableComponent';

If you want to import both the default export and the named export:

import TableComponent, { viewDetails } from './TableComponent';

Finally, you can also access the named import from the default import:

import TableComponent from './TableComponent';
TableComponent.viewDetails();
//TableComponent.js

export const viewDetails = () => {
  return (
    "url here" +
    Array.from(checkedMap.keys()).join() +
    "/details"
  );
};

And in another file you can access this const as below

import { viewDetails } from './TableComponent';
const viewDetails = () => {
  return (
    "url here" +
    Array.from(checkedMap.keys()).join() +
    "/details"
  );
};

export default viewDetails;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744890959a4599410.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信