我有两个独立的元素(不是父母-孩子)。
是否可以完成在单击StyledSearchInput时实际单击StyledDropDownInputAsync的行为。
<StyledSearchInput/>
<StyledDropdownInputAsync searchIcon
className="options"
placeholder="Search"
loadOptions={loadOptions}
onChange={this.handleSelection}
cache={{}}
filterOptions={(options) => (options)}>
</StyledDropdownInputAsync>
将它们包装在父组件中,定义点击状态,将点击状态传递给StyledDropdownInputAsync
,将点击动作传递给StyledSearchInput
。
const ParentComp = () => {
const [isClicked, setClick] = useState(false);
return (
<>
<StyledSearchInput onClick={() => setClick(true)}/>
<StyledDropdownInputAsync isClicked={isClicked} {...otherProps}>
</StyledDropdownInputAsync>
</>
);
};
此在ReactJS中称为Lifting State Up。