Skip to content

Refactor react components#120

Merged
dmitry-korolev merged 7 commits intoTinkoff:developfrom
berezinmv:refactor/react/subscription-components
Sep 18, 2018
Merged

Refactor react components#120
dmitry-korolev merged 7 commits intoTinkoff:developfrom
berezinmv:refactor/react/subscription-components

Conversation

@berezinmv
Copy link
Copy Markdown
Collaborator

No description provided.

@coveralls
Copy link
Copy Markdown

coveralls commented Sep 17, 2018

Pull Request Test Coverage Report for Build 373

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 100.0%

Totals Coverage Status
Change from base Build 366: 0.0%
Covered Lines: 663
Relevant Lines: 663

💛 - Coveralls


return renderComponent({
name: 'Consumer',
renderProps: props,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Сюда можно целиком прокинуть this.props. Вообще это общая рекомендация по PR - по максимуму избавиться от всяких новомодных штук типа спредов и рестов.

appState: null
}

componentWillMount() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Нужен getDerivedStateFromProps, который будет обновлять стейт на изменение map.

import { Subscription } from '../subscription/subscription'

export type Context = { state?: any; app?: Stapp<any, any> }
export type Context = { app: Stapp<any, any> }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

app?:

}
}) =>
createElement(StappContext.Provider, {
value: { app },
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

вообще, если остается только app, наверное стоит напрямую его в value передавать

export type Context = { app: Stapp<any, any> }

export const StappContext = createContext<Context>({})
export const StappContext = createContext<Context>({} as any)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

и тогда будет createContext<Stapp<any, any>| null>(null)

componentWillMount() {
const app = this.props.app

this.subscription = app.subscribe(this.setAppState.bind(this))

This comment was marked as outdated.

import shallowEqual from 'fbjs/lib/shallowEqual'
import { Component, ComponentClass, createElement } from 'react'
import { identity } from 'stapp/lib/helpers/identity/identity'
import { ConsumerClass } from '../context/ConsumerClass'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

И действительно стоит это переименовать, например AppSubscription

Copy link
Copy Markdown
Contributor

@dmitry-korolev dmitry-korolev left a comment

Choose a reason for hiding this comment

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

пыщ

@dmitry-korolev dmitry-korolev added this to the 2.4.0 milestone Sep 17, 2018
…or AppSubscription using getDerivedStateFromProps
@berezinmv berezinmv force-pushed the refactor/react/subscription-components branch from 4dc6258 to b940b1e Compare September 18, 2018 06:56
this.subscription = app.subscribe((state: State) => this.setAppState(state))
}

static getDerivedStateFromProps(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

https://reactjs.org/docs/react-component.html#static-getderivedstatefromprops

Note that this method is fired on every render, regardless of the cause.

Может, подумаем всё-таки над другим способом. Два мапа и shallowEqual на каждый рендер - перебор.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Посмотри, как сделано в create-subscription
https://github.com/facebook/react/blob/master/packages/create-subscription/src/createSubscription.js#L70

На самом деле я бы почти всё сделал как в классе, который возвращает createSubscription, только еще map добавить

const appState = this.state.appState

return renderComponent({
name: 'Consumer',
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Название другое должно быть


export type AppSubscriptionState<Result> = { appState?: Result }

export class AppSubscription<State, Api, Result> extends Component<
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

StappSubscription мне как-то больше нравится :/

m.berezin added 3 commits September 18, 2018 11:57
…or 'map' changed in getDerivedStateFromProps
…tion state inside constructor, move 'map' default value to StappSubscription.defaultProps
@dmitry-korolev dmitry-korolev merged commit c7a6666 into Tinkoff:develop Sep 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants