小编Dev*_*hod的帖子

我可以通过函数组件传递 ref 吗?

-我正在使用功能组件。- 现在我在这里使用 3 个组件,其中一个是父组件,另外 2 个是子组件。- 我需要访问一个子组件方法或状态到另一个子方法。我已经使用CreateRef完成了类组件,但现在我需要使用函数组件,但我在“ref.current”中得到 Null。

export function SideBySideList(props) {
    const ref = React.createRef();

	//this is call inside ListPage after sucess
    function updateRightList(id) {
        ref.current.state.actualSearchedModel.Id = id
        ref.current.fetchDataAndUpdate();
    }
    function itemClicked(id) {
        updateRightList(id);
    }
    return (
        <>
            <div className="col-12 no-padding">
                <div className={props.leftListLayoutClass}>
                    <ListPage
					    updateRightList={updateRightList}
					/>
                </div>
                <div className={props.rightListLayoutClass}>
                    <ListPage
                        ref={ref}
                    />
                </div>
            </div>
        <>
    );
}
Run Code Online (Sandbox Code Playgroud)

dom components function reactjs

4
推荐指数
1
解决办法
9135
查看次数

标签 统计

components ×1

dom ×1

function ×1

reactjs ×1