步骤1:添加“创建新页面”按钮
从“@wordpress/data”导入{useDispatch}; 从“@wordpress/components”导入{Button、Modal、TextControl}; 函数CreatePageButton(){ const[isOpen,setOpen]=使用状态(false); const openModal=()=>设置打开(true); const closeModal=()=>设置打开(false); 返回( <> <Button onClick={openModal}variant=“primary”> 创建新页面 </按钮> {isOpen(打开)&&( <Modal onRequestClose={closeModal}title=“创建新页面”> <创建页面表单 onCancel={closeModal} onSaveFinished={closeModal} /> </模式> ) } </> ); } 函数CreatePageForm(){ //现在是空的 返回<div/>; }
函数MyFirstApp(){ // ... 返回( <div> <div className=“list-controls”> <SearchControl onChange={setSearchTerm}值={searchTerm}/> <创建页面按钮/> </div> <PagesList hasResolved={hasResorved}pages={pages}/> </div> ); }
步骤2:提取受控PageForm
函数EditPageForm({pageId,onCancel,onSaveFinished}){ // ... 返回( <页面形式 title={page.title} onChangeTitle={handleChange} hasEdits={hasEdits\} lastError={lastEror} isSaving={isSaving} onCancel={onCancel} onSave={handleSave} /> ); } 函数PageForm({title,onChangeTitle,hasEdits,lastError,isSaving,onCancel,onSave}){ 返回( <div className=“my-gutenberg-form”> <文本控件 label=“页面标题:” 值={title} onChange={onChangeTitle} /> {last错误( 错误:{lastError.message} ) : ( 假 ) } <div className=“表单按钮”> <按钮 onClick={onSave} variant=“主要” disabled={!hasEdits|isSaving} > {正在保存吗( <> <微调器/> 正在保存 </> ):“保存”} </按钮> <按钮 onClick={onCancel} variant=“三级” disabled={isSaving} > 取消 </按钮> </div> </div> ); }
步骤3:构建CreatePageForm
标题 onChangeTitle(更改标题) has编辑 last错误 正在保存 onCancel(取消) onSave(保存)
标题,onChangeTitle,hasEdits
函数CreatePageForm({onCancel,onSaveFinished}){ const[title,setTitle]=useState(); const-handleChange=(title)=>setTitle(标题); 返回( <页面形式 title={title} onChangeTitle={setTitle} hasEdits={!!title} { /* ... */ } /> ); }
onSave,onCancel
saveEntityRecord('postType','page',{title:“测试”});
函数CreatePageForm({onSaveFinished,onCancel}){ // ... const{saveEntityRecord}=useDispatch(coreDataStore); const-handleSave=async()=>{ const savedRecord=等待saveEntityRecord( “postType”, '第页', {标题} ); if(保存的记录){ onSaveFinished(); } }; 返回( <页面形式 { /* ... */ } onSave={handleSave} onCancel={onCancel} /> ); }
函数CreatePageForm({onSaveFinished,onCancel}){ // ... const{saveEntityRecord}=useDispatch(核心数据存储); const-handleSave=async()=>{ const savedRecord=等待saveEntityRecord( “postType”, '第页', {标题,状态:“发布”} ); if(保存的记录){ onSaveFinished(); } }; 返回( <页面形式 { /* ... */ } onSave={handleSave} onCancel={onCancel} /> ); }
last错误,正在保存
函数CreatePageForm({onCancel,onSaveFinished}){ // ... const{lastError,isSaving}=useSelect( (选择)=>({ //请注意缺少pageId参数: lastError:选择(coreDataStore) .getLastEntitySaveError(“postType”,“page”), //注意缺少pageId参数 正在保存:选择(核心数据存储) .isSavingEntityRecord(“postType”,“page”), } ), [] ); // ... 返回( <页面形式 { /* ... */ } lastError={lastEror} isSaving={isSaving} /> ); }
将其连接在一起
函数CreatePageForm({onCancel,onSaveFinished}){ const[title,setTitle]=useState(); const{lastError,isSaving}=使用选择( (选择)=>({ lastError:选择(coreDataStore) .getLastEntitySaveError(“postType”,“page”), 正在保存:选择(核心数据存储) .isSavingEntityRecord(“postType”,“page”), } ), [] ); const{saveEntityRecord}=useDispatch(核心数据存储); const-handleSave=async()=>{ const savedRecord=等待saveEntityRecord( “postType”, '第页', {标题,状态:“发布”} ); if(保存的记录){ onSaveFinished()上; } }; 返回( <页面形式 title={title} onChangeTitle={setTitle} hasEdits={!!title} onSave={handleSave} lastError={lastEror} onCancel={onCancel} isSaving={isSaving} /> ); } 函数EditPageForm({pageId,onCancel,onSaveFinished}){ const{page,lastError,isSaving,hasEdits}=useSelect( (选择)=>({ 页面:select(coreDataStore).getEditedEntityRecord('postType','page',pageId), lastError:选择(coreDataStore).getLastEntitySaveError(“postType”,“page”,pageId), isSaving:选择(coreDataStore).isSavingEntityRecord('postType','page',pageId), hasEdits:select(coreDataStore).hasEditsForEntityRecord('postType','page',pageId), } ), [页面ID] ); const{saveEditedEntityRecord,editEntityRegord}=useDispatch(核心数据存储); const-handleSave=async()=>{ const savedRecord=等待saveEditedEntityRecord(“postType”,“page”,pageId); if(保存的记录){ onSaveFinished(); } }; const handleChange=(title)=>editEntityRecord('postType','page',page.id,{title}); 返回( <页面表单 title={page.title} onChangeTitle={handleChange} hasEdits={hasEdits\} lastError={lastEror} isSaving={isSaving} onCancel={onCancel} onSave={handleSave} /> ); } 函数PageForm({title,onChangeTitle,hasEdits,lastError,isSaving,onCancel,onSave}){ 返回( <div className=“my-gutenberg-form”> <文本控件 label=“页面标题:” 值={title} onChange={onChangeTitle} /> {lastError( 错误:{lastError.message} ) : ( 假 ) } <div className=“表单按钮”> <按钮 onClick={onSave} variant=“主要” disabled={!hasEdits|isSaving} > {正在保存吗( <> <微调器/> 正在保存 </> ):'保存'} </按钮> <按钮 onClick={onCancel} variant=“三级” disabled={isSaving} > 取消 </按钮> </div> </div> ); }