personal-media-lib's Introduction
personal-media-lib's People
personal-media-lib's Issues
how to get route update from provider without having it redraw
I'm trying to read the route from the provider, which works, but it means that its state is reset each time the route updates. I want to keep the persistent provider but receive update from the router. If I move up the provider outside the route, it doesn't receive route props.
Do not overuse state
In PlaylistContext.js , you are overusing state
import React, { useState,createContext } from 'react';
import myData from './data.json';
const {tracks} = myData;
export const PlaylistContext = createContext();
export class PlaylistProvider extends React.Component {
// avoid using constructor
// rather use class properties/methods (handled by Babel plugin "transform-class-properties")
// state = {}
// getListData(currentFolder, favoriteTracks) { ... }
constructor(props){
super(props);
this.getListData = (currentFolder,favoriteTracks) => {
if(favoriteTracks){
return this.mapRecursive(tracks.children).filter((track)=>track.favorite);
}else{
return [...currentFolder.children];
}
}
this.mapRecursive = (trackList) => {
// This shoud be a class method
let output = [];
trackList.map((track)=>{
output.push(track);
if(track.children){
// No need to spread
// => output = this.mapRecursive(track.children)
// !!! Be aware you are reseting `output` !!!
output = [...this.mapRecursive(track.children)];
}
});
return output;
}
this.state = {
currentFolder: tracks,
parentFolders: [],
selected: tracks.children.filter(track => !track.children)[0],
sideDrawer: false,
favoriteTracks: props.match.path == "/favorite",
importOpen: false,
displayedItems: this.getListData(tracks,props.match.path == "/favorite"),
playerRef: undefined,
// All functions should be class methods
onListClick: (track) => {
console.log("onListClick");
if(track === undefined){
this.state.navigateUp();
}else if(track.children){
this.state.navigateToFolder(track);
}else{
this.setState({selected: track});
this.state.restartPlayer();
}
},
navigateToFolder: (track) =>{
console.log("navigateToFolder");
if(track.children){
this.setState(state => ({currentFolder: track,parentFolders: [...state.parentFolders,state.currentFolder],displayedItems: this.getListData(track,false)}));
}
},
navigateUp: () => {
let parents = [...this.state.parentFolders];
const newCurrent = parents.splice(parents.length-1, 1)[0];
this.setState(state => ({currentFolder: newCurrent,parentFolders: parents,displayedItems: this.getListData(newCurrent,false)}));
},
toggleDrawer: (open) => {
//if(this.state.sideDrawer !== open){
this.setState({sideDrawer: open});
//}
},
onNextClick: () =>{
const children = this.state.displayedItems;
const index = children.indexOf(this.state.selected)+1;
const boundaries = index === children.length ? 0: index;
for(let newIndex = boundaries;newIndex < children.length;newIndex++){
if(!children[newIndex].children){
this.setState({selected: children[newIndex]});
this.state.restartPlayer();
return;
}
}
},
onPrevClick: () =>{
const children = this.state.displayedItems.filter(track => !track.children);
const index = children.indexOf(this.state.selected) -1;
const boundaries = index < 0 ? children.length-1: index;
for(let newIndex = boundaries;newIndex >= 0;newIndex--){
if(!children[newIndex].children){
this.setState({selected: children[newIndex]});
this.state.restartPlayer();
return;
}
}
},
setPlayerRef: (node) =>{
// This is not invoked...
// Not sure about the goal of this function, but
// avoid storing HTML node ref into state
// In your JSX, just :
// <div ref={node => this.playerRef = node} />
// You'll then be allowed to access your class
// Alternate new method (prefered):
// this.playerRef = React.createRef()
// // later...
// <div ref={this.playerRef} />
if(node != this.state.playerRef && node != null){
this.setState({playerRef: node});
}
},
restartPlayer: () =>{
if(this.state.playerRef != undefined){
this.state.playerRef.load();
this.state.playerRef.play();
}
},
setFavoriteTracks: (value) =>{
this.setState(state=>({favoriteTracks: value,displayedItems: this.getListData(state.currentFolder,value)}))
},
setImportOpen: (value) =>{
this.setState({importOpen: value});
},
onListFavoriteClick: (track) =>{
track.favorite = track.favorite ?!track.favorite: true;
this.setState(state =>({displayedItems: this.getListData(state.currentFolder,state.favoriteTracks)}));
},
};
//this.setState({displayedItems: ,});
}
render (){
// Now I understand better why you are using state as much...
// Think before using context, there probably other way to do so
// Newer versions of React let you use hooks, which are kind
// of (reusable) helpers to add super powers to your components
return (
<PlaylistContext.Provider value={this.state}>
{this.props.children}
</PlaylistContext.Provider>
);
}
}
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.