简体   繁体   English

我如何准确地知道存在什么样的“输入”道具变量?

[英]How do I know precisely what kind of “input” prop variables are there?

I'm from Java background and I'm trying hard to learn web language: React.我来自 Java 背景,我正在努力学习 web 语言:React。 I remember in java, function is defined with set of inputs: public static int add(int x, int y) However, in React there seem to be only one (props) and all inputs are used as prop.x prop.y ... etc I remember in java, function is defined with set of inputs: public static int add(int x, int y) However, in React there seem to be only one (props) and all inputs are used as prop.x prop.y . .. ETC

I'm importing the following Map component in 'kakao-map-react' package:我在“kakao-map-react”package 中导入以下 Map 组件:

var Map = function (props) {
    var id = props.id;
    var kakao = window.kakao;
    var mapId = id ? id : 'kakao-map-react';
    var map = useMapState().map;
    var dispatch = useMapDispatch();
    React.useEffect(function () {
        if (!map) {
            var events_1 = [];
            var mapContainer = document.getElementById(mapId);
            var mapOptions = {
                center: new kakao.maps.LatLng(props.initialPosition.latitude, props.initialPosition.longitude),
                level: props.level,
            };
            var newMap_1 = new kakao.maps.Map(mapContainer, mapOptions);
            if (props.onMapLoaded) {
                props.onMapLoaded(newMap_1);
            }
            var _loop_1 = function (event_1) {
                if (props[event_1.key]) {
                    kakao.maps.event.addListener(newMap_1, event_1.event, function (mouseEvent) {
                        if (props[event_1.key]) {
                            if (event_1.hasMouseEvent) {
                                var handler = props[event_1.key](newMap_1, mouseEvent);
                                events_1.push({
                                    target: newMap_1,
                                    type: event_1.event,
                                    handler: handler,
                                });
                            }
                            else {
                                var handler = props[event_1.key](newMap_1);
                                events_1.push({
                                    target: newMap_1,
                                    type: event_1.event,
                                    handler: handler,
                                });
                            }
                        }
                    });
                }
            };
            for (var _i = 0, eventsArr_1 = eventsArr; _i < eventsArr_1.length; _i++) {
                var event_1 = eventsArr_1[_i];
                _loop_1(event_1);
            }
            dispatch({
                type: 'SET_MAP',
                payload: newMap_1,
            });
            return function () {
                for (var _i = 0, events_2 = events_1; _i < events_2.length; _i++) {
                    var event_2 = events_2[_i];
                    kakao.maps.event.removeListener(event_2.target, event_2.type, event_2.handler);
                }
            };
        }
        else {
            return function () {
            };
        }
    }, []);
    /*
    * Set new center if long lat changes
    * */
    React.useEffect(function () {
        if (map) {
            map.setCenter(new kakao.maps.LatLng(props.center.latitude, props.center.longitude));
        }
    }, [
        props.center.latitude,
        props.center.longitude,
        props.center.token,
    ]);
    /*
    * Set new level if props.level changes
    * */
    React.useEffect(function () {
        if (map) {
            map.setLevel(props.level);
        }
    }, [
        props.level
    ]);
    return (React.createElement("div", { id: mapId, style: {
            height: '100%'
        } }, props.children));
};

In this long code, how do I know what kind of inputs(props) I need to insert?在这个长代码中,我怎么知道我需要插入什么样的输入(道具)?

I tried Ctrl-F 'props', but there were too many and I didn't understand which input is which:我试过 Ctrl-F 'props',但是太多了,我不明白哪个输入是哪个:

props.id, props.initialPosition.latitude, props.initialPosition.longitude, props.level, props.onMapLoaded, props[event_1.key], props.center.latitude, props.center.longitude, props.center.token.

Thank you.谢谢你。

I have a Java background myself and was surprised to see that many documentations in JavaScript world don't care about about the data types in their API references.我自己有一个 Java 背景,很惊讶地看到 JavaScript 世界中的许多文档并不关心其 API 引用中的数据类型。 You have two options:你有两个选择:

  1. Go through their documentation on GitHub or their official site. Go 通过他们在 GitHub 或其官方网站上的文档。 A well-mantained project lists their input props and which types they require.一个维护良好的项目会列出他们的输入道具以及他们需要的类型。

  2. You don't find that information in the documentation => You must dig through the source code and find out yourself.您在文档中找不到该信息 => 您必须深入挖掘源代码并找出自己。

  3. Your IDE might be abale to help you.您的 IDE 可能会为您提供帮助。 I use Intellij and for most packages the IDE is able to infer the types by their typescript definitions.我使用 Intellij,对于大多数软件包,IDE 能够通过其 typescript 定义推断类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM