Skip to content

Conversation

@joelamb
Copy link

@joelamb joelamb commented Oct 1, 2018

I seem to be passing a lot of functions and states around, and I feel that it could be more straight-forward.
Especially with toggling styles and getting the search form to minimise only on the first user interaction.

this.setState({
filmDetails: body,
hints: []
}, () => this.toggleVisible())
Copy link
Contributor

Choose a reason for hiding this comment

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

I would probably use isClosed: !this.state.isClosed in setState to avoid having to use a callback

query: query,
currentPage: 1,
filmDetails: {}
}, () => this.searchByTitle(query, this.state.currentPage));
Copy link
Contributor

Choose a reason for hiding this comment

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

I would take the search function out of the callback and pass in a hardcoded page number. Since searchByTitle only uses params and not state inside it, it should be fine

receiveSearchHint(string) {
this.setState({
query: string,
}, () => this.searchHint(this.state.query));
Copy link
Contributor

Choose a reason for hiding this comment

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

As above, I would take searchHint out of the callback and use this.searchHint(string). It avoids having to use the callback

}, () => this.searchByTitle(this.state.query, this.state.currentPage));
}

receiveFavListState(boolean) {
Copy link
Contributor

Choose a reason for hiding this comment

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

boolean is a bit ambiguous. I would give the param a more specific name

favourites.unshift(obj);
this.setState({
favourites: favourites
}, () => window.localStorage.setItem("favourites", JSON.stringify(this.state.favourites)));
Copy link
Contributor

Choose a reason for hiding this comment

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

I would take window.localStorage.setItem("favourites", JSON.stringify(this.state.favourites)) out of callback and use window.localStorage.setItem("favourites", JSON.stringify(favourites))

favourites.splice(index, 1);
this.setState({
favourites: favourites
}, () => window.localStorage.setItem("favourites", JSON.stringify(this.state.favourites)));
Copy link
Contributor

Choose a reason for hiding this comment

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

As above

this.handleClick = this.handleClick.bind(this);
}

handleClick(e) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would probably create two separate click handlers, one for each action rather than checking classes. It would make the code a tiny bit easier to read

handleClick(e) {
this.setState({
showFavs: !this.state.showFavs
}, () => this.props.receiveFavListState(this.state.showFavs))
Copy link
Contributor

Choose a reason for hiding this comment

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

I would take the receiver call out of callback and pass to it a pre-calcualated value

handleClick(e) {
const totalPages = Math.ceil(this.props.totalFilms / this.props.filmsPerPage)

if (e.target.className.includes("btn__next") && this.state.currentPage < totalPages) {
Copy link
Contributor

Choose a reason for hiding this comment

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

As before, I would suggest separate click handlers and not using setState callbacks

}

handleClick(e) {
this.props.receiveFilmID(e.target.parentNode.dataset.id);
Copy link
Contributor

Choose a reason for hiding this comment

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

It's better practice not use data attributes in React and instead work with data from props

@dmitrigrabov
Copy link
Contributor

Good work, but I would recommend avoiding setState callbacks where possible. Also, using more click handlers will save having to write logic which checks classes

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.

2 participants