【发布时间】:2020-05-02 23:36:30
【问题描述】:
所以我有一个功能组件,它是一个工具栏。它的父级是一个文本编辑器。工具栏有许多子组件,它们是按钮。单击其中一个按钮时,我希望出现一个模式。 useModal的逻辑。但是FormatToolbarModal 没有出现。
我读过一个组件的所有渲染都必须由顶级自定义组件完成?但我不确定从那里去哪里。我希望此模式可重复使用,因为工具栏中的其他选项将使用它。
index.jsx
ReactDOM.render(routes, document.getElementById("app"));
App.jsx
const App = () => {
return (
<TextEditor/>
)
}
TextEditor.jsx
const TextEditor = () => {
return(
<FormatToolbar>
<FormatToolbarBlock format="link" icon={link2} />
<FormatToolbarBlock format="image" icon={image} />
</FormatToolbar>
... Editor stuff
)
}
const FormatToolbarBlock = ({ format, icon }) => {
const editor = useSlate();
const {isShowing, toggle} = useModal();
return (
<FormatButton
onMouseDown={e => {
if(format === 'link'){
toggle(e);
<FormatToolbarModal <---- here is my issue
isShowing={isShowing}
hide={toggle}
/>
} else if {
...
}
}}
/>
)
}
UseModal.jsx
const useModal = () => {
const [isShowing, setIsShowing] = useState(false);
function toggle() {
setIsShowing(!isShowing);
}
return {
isShowing,
toggle,
}
};
export default useModal;
FormatToolbarModal.jsx
const FormatToolbarModal = ({ isShowing, hide }) => isShowing ? ReactDOM.createPortal(
<React.Fragment>
<p>I am a modal</p>
</React.Fragment>, document.body
): null;
export default FormatToolbarModal;
希望从这里,您可以更清楚地看到我的问题。我是 React 和 hooks 的新手,因此欢迎提出任何建议。
谢谢!
【问题讨论】:
标签: javascript reactjs react-hooks