在 React 中构建一款可靠的 Pokémon 游戏:开发者的冒险!
收藏
亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《在 React 中构建一款可靠的 Pokémon 游戏:开发者的冒险!》很感兴趣。本篇文章就来给大家详细解析一下,主要介绍一下,希望所有认真读完的童鞋们,都有实质性的提高。

s:单一职责 – 一只神奇宝贝,一份工作
问题:pokemoncomponent 处理捕捉、战斗和显示分数,违反了 srp。
function pokemoncomponent({ pokemon, oncatch, onbattle, score }) {
return (
<div>
<h2>{pokemon.name}</h2>
<button onclick={() => oncatch(pokemon)}>catch</button>
<button onclick={() => onbattle(pokemon)}>battle</button>
<div>score: {score}</div>
</div>
);
}
解决方案:责任划分。
function pokemoncatcher({ pokemon, oncatch }) {
return <button onclick={() => oncatch(pokemon)}>catch</button>;
}
function pokemonbattler({ pokemon, onbattle }) {
return <button onclick={() => onbattle(pokemon)}>battle</button>;
}
function scoreboard({ score }) {
return <div>score: {score}</div>;
}
function pokemongame({ pokemon, oncatch, onbattle, score }) {
return (
<div>
<h2>{pokemon.name}</h2>
<pokemoncatcher pokemon={pokemon} oncatch={oncatch} />
<pokemonbattler pokemon={pokemon} onbattle={onbattle} />
<scoreboard score={score} />
</div>
);
}
o:开放/封闭 – 进化的神奇宝贝组件
问题:添加电源等功能需要修改现有组件。
解决方案:使用高阶组件(hoc)。
function withpowerup(pokemoncomponent) {
return function poweredupcomponent(props) {
const [ispoweredup, setpowerup] = usestate(false);
const powerup = () => {
setpowerup(true);
settimeout(() => setpowerup(false), 5000);
};
return (
<div>
<pokemoncomponent {...props} ispoweredup={ispoweredup} />
<button onclick={powerup}>power up!</button>
</div>
);
};
}
const charmander = ({ ispoweredup }) => (
<div>charmander {ispoweredup && "(powered up!)"}</div>
);
const poweredcharmander = withpowerup(charmander);
function pokemonapp() {
return <poweredcharmander />;
}
l:里氏替换 – 可互换的神奇宝贝
问题:交换组件会导致问题。
解决方案:使用基础组件。
function basepokemon({ attack, children }) {
return (
<div classname="pokemon">
<div>attack: {attack}</div>
{children}
</div>
);
}
function pikachu({ attack }) {
return (
<basepokemon attack={attack}>
<h2>pikachu</h2>
</basepokemon>
);
}
function charizard({ attack }) {
return (
<basepokemon attack={attack}>
<h2>charizard</h2>
</basepokemon>
);
}
function pokemonbattle() {
return (
<div>
<basepokemon attack="tackle">
<h2>generic pokémon</h2>
</basepokemon>
<pikachu attack="thunderbolt" />
<charizard attack="flamethrower" />
</div>
);
}
d:依赖倒置 – 依赖于抽象
问题:组件与数据源紧密耦合。
解决方案:使用上下文进行数据注入。
const PokemonContext = createContext();
function Pikachu() {
const { attack } = useContext(PokemonContext);
}
<PokemonContext.Provider value={{ attack: "Thunderbolt" }}>
<Pikachu />
</PokemonContext.Provider>
备忘单:坚实的原则
| principle | poké-mantra | trainer’s tip |
|---|---|---|
| single responsibility | one pokémon, one role. | split complex components into focused ones. |
| open/closed | evolve without changing. | use hocs, render props for new features. |
| liskov substitution | components like pokémon moves – interchangeable. | ensure components can be used interchangeably. |
| dependency inversion | depend on abstractions, not concretes. | use context or props for data management. |
今天关于《在 React 中构建一款可靠的 Pokémon 游戏:开发者的冒险!》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注米云公众号!
版本声明 本文转载于:dev.to 如有侵犯,请联系删除
- 为什么在 Eclipse 编写 JavaScript 时没有自动提示?
