-
Notifications
You must be signed in to change notification settings - Fork 96
Lesson 8 #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| symbol: "Y", | ||
| }, | ||
| player2: { | ||
| name: "", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Классика name: "Alice" =)
|
|
||
| export const forms = () => ( | ||
| <StoryWrapper> | ||
| <DemoForm FormComponent={GameSettingsFormDOM} title="GameSettingsFormDOM" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FormComponent -> children?
<DemoForm title="GameSettingsFormRef">{GameSettingsFormRef}</DemoForm>There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
см выше - DemoForm не просто для визуализации обертка
| margin-right: 10px; | ||
| `; | ||
|
|
||
| const DemoForm: React.FC<DemoFormProps> = ({ FormComponent, title }) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const DemoForm: React.FC<DemoFormProps> = ({ children: FormComponent, title }) => {There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
я еще пробрасываю ref и callback - тогда children придется переименовывать (компонент должен быть с большой буквы)
те
{children} // ok
<Children onSubmit={} /> // не ок
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
children: FormComponent вроде смапит имя или нет?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
смапит, но это же не children )) children (по крайней мере в моей практике) - дочерние элементы, которые рендерятся как есть
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Хорошо, принял + понял )
| @@ -0,0 +1,91 @@ | |||
| import React from "react"; | |||
| import { GameSettingsFormProps } from "./interfaces"; | |||
|
|
|||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const getInputValue = (target: HTMLFormElement, name: string) =>
(target.querySelector(`[name=${name}]`) as HTMLInputElement).value
//...
this.props.onSubmit({
player1: {
name: getInputValue("player1Name")
//...|
|
||
| render() { | ||
| return ( | ||
| <form onSubmit={this.handleSubmit}> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dump component form?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
нет, суть в том, чтобы компонент наружу отдавал чистые данные (все значения)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Я имел в виду что то вроде того
export const Player = ({ children: playerName, ... }) <fieldset>
<legend>Game Settings</legend>
<fieldset>
<legend>{playerName}</legend>
<label>
Name:
<input
name={playerName}
type="text"
placeholder={`${playerName} name`}
required
/>
</label>
//...
<form onSubmit={this.handleSubmit}>
<Player prop1="some" ...>Player1</Player>There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
в реальном проекте (ну кроме того, что я бы так не писал - возможно да)
но тут через все компоненты я стараюсь провести одну структуру (с минимальными изменениями, кроме подхода к работе с данными)
а этот подход сработает только в случает с DOM ( на рефах и стейте будет уже совсем другое)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А если прокинуть ref или обработчик сверху? Думаю привевать композицию с самого начала студентам надо )
|
|
||
| render() { | ||
| return ( | ||
| <form onSubmit={this.handleSubmit}> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- dump component form ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
тоже что и выше )
| import React from "react"; | ||
| import { GameSettingsFormProps } from "./interfaces"; | ||
|
|
||
| interface GameSettingsFormStateState { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Proposal
Можно было бы везде использовать один тип, тоесть возможно его стоило бы разместить в src/types
export interface Player {
name: string;
color: string;
symbol: string;
}И стейт
interface GameSettingsFormState {
player1: Player;
player2: Player;
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Долистал до https://github.com/nickovchinnikov/react-js-tutorial/pull/14/files#diff-fc42a5bbd7418b6789d5ac5e3c143976
А почему его не использовать бы тут?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
потому что это эволюционное изменение прошлых компонент - чтобы показать разницу с минимальными изменениями (все идет к Formik, где так и есть)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ИМХО структура данных может быть не привязана к view
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
полностью согласен, НО (=) ):
в примере с DOM / REF струтура плоская (что определяется самой формой хранения данных - plain html), и здесь я не хочу перескакивать ступеньку (один шаг - переход на state, второй шаг - держать вложенные данные в стейте )
Могу просто сделать еще одну форму для этого шага
| this.setState( | ||
| { | ||
| [prop]: (ev.target as HTMLInputElement).value, | ||
| } as any /** don't do this in real projects. Better to have multiple setters */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as GameSettingsFormState ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
это не совместимые типы -только если через unkown кастовать, тут меня вообще очень сильно удивили тайпинги для setState - он даже Partial<GameSettingsFormStateState> не хочет принимать
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Странно, действительно (
|
|
||
| render() { | ||
| return ( | ||
| <form onSubmit={this.handleSubmit}> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dump component form ?
я думаю, это можно сделать "глупыми" компонентом для всех случаев
тут просматривается несколько компонентов Player, можно красиво скомпозировать ИМХО
No description provided.