Skip to content

Evgenii/lesson7#13

Merged
nickovchinnikov merged 5 commits into
masterfrom
evgenii/lesson7
Apr 27, 2020
Merged

Evgenii/lesson7#13
nickovchinnikov merged 5 commits into
masterfrom
evgenii/lesson7

Conversation

@EJohnF
Copy link
Copy Markdown

@EJohnF EJohnF commented Apr 22, 2020

No description provided.

Comment thread src/lesson7/ImageLocal.tsx Outdated

static getDerivedStateFromProps(props: ImageProps) {
return {
url: `https://picsum.photos/id/${props.id}/200`,
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Шаблон было бы здорово вынести, встречатеся несколько раз по коду данная ссылка

Comment thread src/lesson7/ImageRandom.tsx Outdated

const getUrl = (id: number): string => {
const goodId = Math.min(1000, Math.max(1, id));
return `https://picsum.photos/id/${goodId}/200`;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ссылка, конфиг

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тогда шаблонную фукнцию создавать нужно - тут же просто разные примеры

Copy link
Copy Markdown
Owner

@nickovchinnikov nickovchinnikov Apr 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const getUrlWithID = (id: number) => `https://picsum.photos/id/${id}/200`

Comment thread src/lesson7/ImageRandom.tsx
Comment thread src/lesson7/Screen.tsx Outdated
@@ -0,0 +1,66 @@
import React from "react";
import { ImageServer as Image } from "./ImageServer";
// import { ImageLocal as Image} from "./ImageLocal";
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Закоментированный код

Comment thread src/lesson7/Screen.tsx
}

export class App extends React.Component<{}, ScreenState> {
constructor({}) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constructor({}) { super({});

В данном случае подобное использование оправданно?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Либо пустой объект же, либо null отправлять в super, не так ли? Когда у нас нет пропсов.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constructor({}) { <- это деструктурация https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment (Unpacking fields from objects passed as function parameter)
ИМХО это может запутать, сперва идет диструктуразация, причем пустая и это нигде не используется.

Почему не так?

constructor(props) { super(props);

// or

constructor() { super({});

// or

constructor() { super(null));

Comment thread src/lesson7/Screen.tsx Outdated
render() {
const { id, interval, isActive } = this.state;
return (
<div style={{ textAlign: "center" }}>
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emotion styled?

Comment thread src/lesson7/ImageServer.tsx Outdated

componentDidMount() {
getUrl(this.props.id).then((url) => {
this.setState({ url });
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

вот я пропустил на занятии - было рассказано, что так можно получать ошибки, и что хорошо бы делать проверку? (я про случай, когда компонент удалили до резолва)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

И я про это забыл! Спасибо что напомнил. Сейчас напишу и нужно будет на следующем занятии об этом тоже сказать бы...

Comment thread src/lesson7/ImageServer.tsx Outdated

componentDidUpdate(prevProps: ImageProps) {
if (prevProps.id !== this.props.id) {
getUrl(this.props.id).then((url) => {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ну и да, может это в метод вынести?

…revention of setting state on unmounted component, add emotion.
Comment thread src/lesson7/utils.ts
@@ -0,0 +1,12 @@
export const getUrl = (id: number): string => {
const goodId = Math.min(1000, Math.max(1, id));
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Как вариант, немного читабельней
Math.min([Math.abs(id), 1000]);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

проверка - что вернётся в случае id = 0?

0 - не верный id

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Я отталкивался от тесткейсов, такого теста в списке нету (

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Стоит исправить тесты в таком случае, я вижу 4 кейса: -1, 0, 1, 1001
@EJohnF Ничего не упустил?

Comment thread src/lesson7/utils.ts Outdated
@@ -0,0 +1,12 @@
export const getUrl = (id: number): string => {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Может стоит

export const ImageLink = "https://picsum.photos/id";
export const DefaultImageSize = 200;

type GetURLFunction = (id: number, size?: number) => string;

export const getUrl: GetURLFunction = (id, size = DefaultImageSize) => {
//...
 return `${ImageLink}/${goodId}/${size}`;

В этом случае можно очень красиво написать тесты, ИМХО

import { ImageLink, DefaultImageSize, getAsyncUrl, getUrl } from "./utils";

describe("Image utils", () => {
  describe("Get local transformed links", () => {
    it("test for correct id range 1-1000", () => {
      const id = 1;
      expect(getUrl(id)).toBe(`${ImageLink}/${id}/${DefaultImageSize}`);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

И меняется ссылка в этом случае оч легко

@nickovchinnikov nickovchinnikov merged commit d892dc2 into master Apr 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants