Kory Tegman
senior software engineer
Kory Tegman,
Button that Increments
const { useState } = React;
const App = () => {
const [count, setCount] = useState(0)
return <div>
<button onClick={() => setCount(count+1)} >increment</button>
<br />
count: {count}
</div>
}
ReactDOM.render(<App />, document.getElementById('app'))
import React, { Component } from "react";
class Main extends Component {
state = {
text: "HELLO!"
};
render() {
return <First text={this.state.text} />;
}
}
const First = props => <Second text={props.text} />
const Second = props => <Third text={props.text} />
const Third = props => <h3>Text is: {props.text}</h3>
export default Main;
import React from "react";
import { getUser } from "api/users";
const AppContext = React.createContext(null);
const App = () => {
const user = getUser();
return (
<AppContext.Provider value={user}>
<UserProfilePage />
</AppContext.Provider>
);
};
const UserProfilePage = () => {
return (
<AppContext.Consumer>
{user => <UserHeader user={user} />}
</AppContext.Consumer>
);
};
const AppContext = React.createContext(null);
const App = () => {
const user = getUser();
return (
<AppContext.Provider value={user}>
<UserProfilePage />
</AppContext.Provider>
);
};
const UserProfilePage = () => {
const user = React.useContext(AppContext);
return (
<React.Fragment>
<UserHeader user={user} />
{/* Render rest of your user page */}
</React.Fragment>
);
};
const AppContext = React.createContext(null);
const currentUserReducer = (state, action) => {
switch (action.type) {
case "UPDATE_AVATAR": {
return { ...state, avatar: action.avatar };
}
default: {
throw new Error(`passed invalid or unknown action.type: ${action.type}`);
}
}
};
const CurrentUserProvider = () => {
const user = getUser();
const [state, dispatch] = React.useReducer(currentUserReducer, user);
return <AppContext.Provider value={[state, dispatch]} />;
};
const App = () => {
return (
<CurrentUserProvider>
<UserProfilePage />
</CurrentUserProvider>
);
};
const UserProfilePage = () => {
const [user, dispatch] = React.useContext(AppContext);
return <React.Fragment>
<UserProfileInfo userName={user.name} userEmail={user.email} />
<UserProfileBody user={user} />
</React.Fragment>
};
const useCurrentUser = () => {
const [state, dispatch] = React.useContext(AppContext);
const updateAvatar = async avatar => {
let user = await updateUser({ userId: state.id, avatar });
dispatch({ type: "UPDATE_AVATAR", avatar: user.avatar });
};
return {state, dispatch, updateAvatar}
}
const UserProfilePage = () => {
const {state, updateAvatar} = useCurrentUser();
return <React.Fragment>
<UserProfileInfo userName={state.name} userEmail={state.email} />
<UserProfileAvatar userAvatar={state.avatar} updateAvatar={updateAvatar} />
{/* render the rest of the profile page */}
</React.Fragment>
};
it’s a technique that executes a (pure) function once, saves the result in memory, and if we try to execute that function again with the same arguments as before, it just returns that previously saved result without executing the function again.
- Sam Pakvis
if anything on our state changes, everything re-renders.
React gives us a way to not re-render what has not changed
const teamsReducer = (state, action) => {
switch (action.type) {
case "TOGGLE_TEAM":
return { ...state, teams: /% update team %/ }
default:
throw new Error(`passed invalid or unknown action.type: ${action.type}`);
}
};
const TeamsContext = createContext(null);
const useTeams = () => {
const [state, dispatch] = useContext(TeamsContext);
const toggleTeam = teamName => {
dispatch({ type: "TOGGLE_TEAM", teamName });
};
return {
state,
dispatch,
toggleTeam,
pickedTeams: state.teams.filter(t => t.picked),
unpickedTeams: state.teams.filter(t => !t.picked)
};
};
const TeamsContextProvider = props => {
const [state, dispatch] = useReducer(teamsReducer, {
teams: Teams
});
return <TeamsContext.Provider value={[state, dispatch]} {...props} />;
};
const App = () => {
return (
<TeamsContextProvider>
<TeamsPage />
</TeamsContextProvider>
);
};
const TeamsPage = () => {
const { pickedTeams, unpickedTeams, toggleTeam } = useTeams();
return (
<ContainerComponent>
<div className="bg-blue-200 w-1/2 m-5 rounded p-4">
{unpickedTeams.map(team => (
<Team key={team.name} action={toggleTeam} team={team} />
))}
</div>
<div className="bg-green-200 w-1/2 m-5 rounded p-4">
{pickedTeams.map(team => (
<Team key={team.name} action={toggleTeam} team={team} />
))}
</div>
</ContainerComponent>
);
};
const Team = ({ team, action }) => {
// logs renders
const renders = useRenders();
return (
<div onClick={() => action(team.name)} >
{team.city} {team.name} - renders: {renders}
</div>
);
};
const Team = React.memo(({ team, action }) => {
// logs renders
const renders = useRenders();
return (
<div onClick={() => action(team.name)} >
{team.city} {team.name} - renders: {renders}
</div>
);
});
const useTeams = () => {
const [state, dispatch] = useContext(TeamsContext);
const toggleTeam = useCallback(
teamName => {
dispatch({ type: "TOGGLE_TEAM", teamName });
},
[dispatch]
);
///.... rest of useTeams////
Sam Pakvis - https://medium.com/@trekinbami/using-react-memo-and-memoization-1970eb1ed128
My wife who has listened to this talk too many times.
By Kory Tegman
it is for a state management in react