简体   繁体   English

是否可以访问 React 组件外部的函数?

[英]Is it possible to access a function outside a component in React?

const MainContainer = () => {

    const muuriRef = useRef();

    // Items state
    const [items_ig, setItems_ig] = useState(generateInstaContainer());
    const [items_fb, setItems_fb] = useState(generateFbContainer());
    const [items_twit, setItems_twit] = useState(generateTwitterContainer());

    // Items as children


    const children_ig = items_ig.map(({id, color, title, width, height}) => (
        <Item_IG
            id={id}
            key={id}
            color={color}
            title={title}
            width={width}
            height={height}
            remove={removeFunction_ig}
        />
    ));

    const children_fb = items_fb.map(({id, color, title, width, height}) => (
        <Item_FB
            id={id}
            key={id}
            color={color}
            title={title}
            width={width}
            height={height}
            remove={removeFunction_fb}
        />
    ));

    const children_twit = items_twit.map(({id, color, title, width, height}) => (
        <Item_TWIT
            id={id}
            key={id}
            color={color}
            title={title}
            width={width}
            height={height}
            remove={removeFunction_twit}
        />

    ));

    function addInstaGram() {
        setItems_ig(items_ig.concat(generateInstaContainer(1)));
    }

    function addFacebook() {
        setItems_fb(items_fb.concat(generateFbContainer(1)));
    }

    function addTwitter() {
        setItems_twit(items_twit.concat(generateTwitterContainer(1)));
    }

    // render
    return (
            <MuuriComponent
                options={{
                    layoutDuration: 400,
                    layoutEasing: "ease",
                    dragEnabled: true,
                    dragSortInterval: 50,
                    dragReleaseDuration: 400,
                    dragReleseEasing: "ease"
                }}
                ref={muuriRef}
            >
                {children_ig}
                {children_fb}
                {children_twit}
            </MuuriComponent>

            <div className="test"><Button
                onClickF={() => addFacebook()}
                onClickT={() => addTwitter()}
                onClickI={() => addInstaGram()}


            /></div>
    );
};

};

I want to use the onClick actions in another component, but I am unsure how to access the functions that would update the grid in this component.我想在另一个组件中使用onClick操作,但我不确定如何访问将更新此组件中网格的函数。 I thought about a callback function, but when I send this component and the newly created callback to the main index the button div is rendered twice because it's returned in the first instance.我想到了一个回调函数,但是当我将此组件和新创建的回调发送到主索引时,按钮 div 呈现两次,因为它是在第一个实例中返回的。 I'm still learning React, I'm mainly familiar with Python.我还在学习React,主要是熟悉Python。 Any help appreciated, thank you!任何帮助表示赞赏,谢谢!

This is what I used for the callback function.这就是我用于回调函数的内容。

const Button = ({onClickI, onClickT, onClickF}) => {
    return ( <div className={someStyles}>
        <button onClick={onClickI}>Test 1</button>
        <button onClick={onClickT}>Test 3</button>
        <button onClick={onClickF}>Test 2</button>
    </div> )

Can You try this你能试试这个吗

const Button = ({onClickI, onClickT, onClickF}) => {
        return ( <div className={someStyles}>
            <button onClick={() => onClickI()}>Test 1</button>
            <button onClick={() => onClickT()}>Test 3</button>
            <button onClick={() => onClickF()}>Test 2</button>
        </div> )

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

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