Udemy - React with Redux Flashcards
What is a connected component?
A React component that’s connected to the Redux store.
What happens when a React component is connected to the Redux store?
The components are going to be able to fetch data off the redux store so they can render something to the screen and when that data that they fetch changes, they’re automatically going to get rerendered so we always have the interface up to date with the latest changes to the redux store. We’ll also be able to dispatch actions directly from our React components. If we have a React component with a from, someone can fill out that data, submit the form and the React component can dispatch he necessary action to change the stores data.
How would you export this code as a function?
const store = createStore( combineReducers({ expenses: expensesReducer, filters: filtersReducer }) );
export default () => { const store = createStore( combineReducers({ expenses: expensesReducer, filters: filtersReducer }) ); return store; };
Why would you export the createStore() function in another function?
When we import the function (the default function (from configureStore.js)), we just call it, we get the store back and we can then use it.
How would you use this function?
export default () => { const store = createStore( combineReducers({ expenses: expensesReducer, filters: filtersReducer }) ); return store; };
import configureStore from ‘./store/configureStore’;
const store = configureStore();
What does store give us access to?
import configureStore from ‘./store/configureStore’;
const store = configureStore();
All the things we had before:
store. dispatch()
store. getState()
store. subscribe()
Give a simple example of using store:
import configureStore from ‘./store/configureStore’;
const store = configureStore();
console.log(store.getState());
How would you add an expense?
store.dispatch(addExpense({ description: ‘Water bill’ }));
How would you set the text filter?
store.dispatch(setTextFilter(‘bill’));
How would you get the visible expenses?
const state = store.getState(); const visibleExpenses = getVisibleExpenses(state.expenses, state.filters); console.log(visibleExpenses);
What is React Redux?
A library that allows us to connect our redux stores to our react components. It makes heavy use of a pattern known as higher order components.
What is a Higher Order Component (HOC)?
A component (HOC) that renders another component (regular component).
NOTE: The HOC may render several other components.
How would you create a HOC that renders < Info / >?
const Info = (props) => ( < div > < h1 >Info< /h1 > < p >The info is: {props.info}.< /p > < /div > );
ReactDOM.render(< Info info=”details” / >, docuent.getElementById(‘app’));
NOTE: We’re implicitly returning some JSX.
NOTE: The HOC is a stateless functional component that implicitly returns some JSX.
const withAdminWarning = (WrappedComponent) => { return (props) => ( < div > < p >This is private info< /p > < /WrappedComponent / > < /div > ); };
const AdminInfo = withAdminWarning(info);
ReactDOM.render(< AdminInfo info=”details” / >, docuent.getElementById(‘app’));
What will HOC allow?
To reuse code.
Able to perform render hijacking.
Add a little prop manipulation.
Abstract state.
What are the steps of creating a HOC?
- Create a (regular) function (not a react component).
- This function gets called with another component that we want to wrap.
- When you call this (HOC) function, what you get back is an alternative version of the component you pass in (it’s going to be the HOC).
- Calling the argument of the (step 1) function, WrappedComponent is a convention. NOTE: it is a component so we do want to start with an upper case first letter.
- Inside this function is where we return a new component. NOTE: The component that is created here is the HOC.
see: The Higher Order Component
What’s the problem with this code?
const withAdminWarning = (WrappedComponent) => { return (props) => ( < div > < p >This is private info< /p > < /WrappedComponent / > < /div > ); };
const AdminInfo = withAdminWarning(info);
ReactDOM.render(< AdminInfo info=”details” / >, docuent.getElementById(‘app’));
The info component is broken. It’s not getting the props that are passed in.
How could you fix this code?
const withAdminWarning = (WrappedComponent) => { return (props) => ( < div > < p >This is private info< /p > < /WrappedComponent / > < /div > ); };
const AdminInfo = withAdminWarning(info);
ReactDOM.render(< AdminInfo info=”details” / >, docuent.getElementById(‘app’));
With the spread operator. When we’re instantiating a component inside of JSX, we can open and close a set of curly braces to create a JavaScript expression. From here, we can spread out any object we like. Here, we spread out props. This has the effect of taking every key value pair on that object and passing them down as props.
const withAdminWarning = (WrappedComponent) => { return (props) => ( < div > < p >This is private info< /p > < /WrappedComponent {...props}/ > < /div > ); };
const AdminInfo = withAdminWarning(info);
ReactDOM.render(< AdminInfo info=”details” / >, docuent.getElementById(‘app’));
How could you provide whether or not you should show the AdminInfo message?
const withAdminWarning = (WrappedComponent) => { return (props) => ( < div > < p >This is private info< /p > < /WrappedComponent {...props}/ > < /div > ); };
const AdminInfo = withAdminWarning(info);
ReactDOM.render(< AdminInfo info=”details” / >, docuent.getElementById(‘app’));
By passing a special prop into the HOC aswell.
NOTE: We use some
const withAdminWarning = (WrappedComponent) => { return (props) => ( < div > { props.isAdmin AND < p >This is private info< /p > } < /WrappedComponent {...props}/ > < /div > ); };
const AdminInfo = withAdminWarning(info);
ReactDOM.render(< AdminInfo isAdmin={true} info=”details” / >, docuent.getElementById(‘app’));
What are we going to be doing a lot with the React Redux library?
We’ll be given a function. We’ll pass out function inside of them and the end result will be a new component that we’re going to be using. This component will have access to the redux store.
How would you create a function that tests whether someone is logged in or not, using this code?
ReactDOM.render(< AuthInfo isAuthenticated={false} info=”details” / >, docuent.getElementById(‘app’));
NOTE: RequireAuthentication() isn’t a HOC, it’s just a regular function that returns the HOC.
NOTE: For our purposes, we return a stateless functional component.
NOTE: We implicitly return some JSX.
const requireAuthentication = (WrappedComponent) => { return (props) => ( < div > {props.isAuthenticated ? ( < WrappedComponent {...props} / > ) : ( < p >Please log in< /p > )} < /div > ); };
How can you install the React Redux library?
yarn add react-redux@x
How many times are we going to use the component?
Once.
How many times are we going to use the < Provider / > component?
Once.
Where are we going to use the < Provider / > component?
At the root of our application.