具有编辑模式和查看模式的 React 组件,这是正确的模式吗?
React component with Edit Mode and View Mode, is this a correct pattern?
我有一个显示食谱的 React 组件。该组件可用于仅查看数据,也可用于编辑数据,具体取决于传递给它的 'edit' 道具。
目前我有一些条件逻辑,这些逻辑将 hide/show 某些元素取决于用户是想编辑还是查看食谱。这是我的渲染函数:
render() {
let buttons = null;
if (this.props.edit === 'true') {
buttons = <div className="buttonList">
<button onClick={this.onSave} className='defaultBtn'>Save</button>
<button onClick={this.goBack} className='defaultBtn'>Discard Changes</button>
</div>;
} else {
buttons = <div className="buttonList">
<button onClick={this.goBack} className='defaultBtn'>Back</button>
</div>;
}
return (
<div className="single">
<img src={this.state.recipe.imageURL} />
<div className='recipeDetails'>
<h1>{this.state.recipe.name}</h1>
{this.props.edit === 'true' > 0 &&
<TextField type='text' floatingLabelText='Image URL' onChange={this.onImageUrlChange}></TextField>
}
<IngredientList onIngredientChange={this.onIngredientChange}
onDelete={this.onDelete}
ingredients={this.state.recipe.ingredients}
edit={this.props.edit}
addIngredient={this.addIngredient} />
{buttons}
</div>
</div>
);
}
这是实现此目标的最佳方法吗?使用这样的 if 语句对我来说感觉不对。我觉得我应该有一个 ViewRecipe 组件和一个 EditRecipe 组件,它们共享大部分代码,但有一些逻辑来隐藏和显示相关元素。是否有某种 React Pattern 可以做到这一点?我已经阅读了一些关于高阶组件的内容,但不确定它们是否适合这个特定问题。
我是不是太复杂了?
我应该只有两个独立的组件吗?
编辑方面的大部分逻辑。
您的道具命名需要审核:
props.edit ='true'
可以是props.mode = 'edit' or 'view'
减轻render
方法的条件逻辑(if...else)并将其分解为以"render"为前缀的方法。
解决方案将是:
renderButtons() {
if (this.props.mode === 'edit')
return (
<div className="buttonList">
<button onClick={this.onSave} className='defaultBtn'>Save</button>
<button onClick={this.goBack} className='defaultBtn'>Discard Changes</button>
</div>
)
else {
return (
<div className="buttonList">
<button onClick={this.goBack} className='defaultBtn'>Back</button>
</div>
)
}
}
renderTextField() {
if (this.props.mode != 'edit') return null;
return (
<TextField type='text' floatingLabelText='Image URL' onChange={this.onImageUrlChange}></TextField>
)
}
render() {
return (
<div className="single">
<img src={this.state.recipe.imageURL} />
<div className='recipeDetails'>
<h1>{this.state.recipe.name}</h1>
{this.renderTextField()}
<IngredientList onIngredientChange={this.onIngredientChange}
onDelete={this.onDelete}
ingredients={this.state.recipe.ingredients}
edit={this.props.edit}
addIngredient={this.addIngredient} />
{this.renderButtons()}
</div>
</div>
);
}
我有一个显示食谱的 React 组件。该组件可用于仅查看数据,也可用于编辑数据,具体取决于传递给它的 'edit' 道具。
目前我有一些条件逻辑,这些逻辑将 hide/show 某些元素取决于用户是想编辑还是查看食谱。这是我的渲染函数:
render() {
let buttons = null;
if (this.props.edit === 'true') {
buttons = <div className="buttonList">
<button onClick={this.onSave} className='defaultBtn'>Save</button>
<button onClick={this.goBack} className='defaultBtn'>Discard Changes</button>
</div>;
} else {
buttons = <div className="buttonList">
<button onClick={this.goBack} className='defaultBtn'>Back</button>
</div>;
}
return (
<div className="single">
<img src={this.state.recipe.imageURL} />
<div className='recipeDetails'>
<h1>{this.state.recipe.name}</h1>
{this.props.edit === 'true' > 0 &&
<TextField type='text' floatingLabelText='Image URL' onChange={this.onImageUrlChange}></TextField>
}
<IngredientList onIngredientChange={this.onIngredientChange}
onDelete={this.onDelete}
ingredients={this.state.recipe.ingredients}
edit={this.props.edit}
addIngredient={this.addIngredient} />
{buttons}
</div>
</div>
);
}
这是实现此目标的最佳方法吗?使用这样的 if 语句对我来说感觉不对。我觉得我应该有一个 ViewRecipe 组件和一个 EditRecipe 组件,它们共享大部分代码,但有一些逻辑来隐藏和显示相关元素。是否有某种 React Pattern 可以做到这一点?我已经阅读了一些关于高阶组件的内容,但不确定它们是否适合这个特定问题。
我是不是太复杂了? 我应该只有两个独立的组件吗? 编辑方面的大部分逻辑。
您的道具命名需要审核:
props.edit ='true'
可以是props.mode = 'edit' or 'view'
减轻
render
方法的条件逻辑(if...else)并将其分解为以"render"为前缀的方法。解决方案将是:
renderButtons() { if (this.props.mode === 'edit') return ( <div className="buttonList"> <button onClick={this.onSave} className='defaultBtn'>Save</button> <button onClick={this.goBack} className='defaultBtn'>Discard Changes</button> </div> ) else { return ( <div className="buttonList"> <button onClick={this.goBack} className='defaultBtn'>Back</button> </div> ) } } renderTextField() { if (this.props.mode != 'edit') return null; return ( <TextField type='text' floatingLabelText='Image URL' onChange={this.onImageUrlChange}></TextField> ) } render() { return ( <div className="single"> <img src={this.state.recipe.imageURL} /> <div className='recipeDetails'> <h1>{this.state.recipe.name}</h1> {this.renderTextField()} <IngredientList onIngredientChange={this.onIngredientChange} onDelete={this.onDelete} ingredients={this.state.recipe.ingredients} edit={this.props.edit} addIngredient={this.addIngredient} /> {this.renderButtons()} </div> </div> ); }