开源软件名称(OpenSource Name):learn-co-curriculum/intro-to-redux-library-codealong开源软件地址(OpenSource Url):https://github.com/learn-co-curriculum/intro-to-redux-library-codealong开源编程语言(OpenSource Language):HTML 46.0%开源软件介绍(OpenSource Introduction):Intro to Redux: Reading Data from StateObjectives
IntroductionIn the previous section, we used a createStore() method that we wrote, and passed a reducer to it. We used the dispatch method from the store to dispatch actions and update the state. Now let's think about which part of our application would belong in the official Redux library -- that is, which part of our codebase would be common to all applications. Well, probably not the reducer as our reducers seem unique to each React & Redux application. The reducers are unique because sometimes we have reducers that would add or remove items, or add or remove users, or edit users, etc. What these actions are and how the reducer manages the state is customized. Thus, the reducer would not be part of the Redux library that other developers would use to build their application. The createStore(), method however is generic across Redux applications. It always returns a store (given a reducer) that will have a dispatch method and a getState method. So from now on, we will import our createStore() method from the official
Redux library. Normally, to install Redux into a React application, you need to
install two packages, In this code along, we'll be building a simple counter application that displays the value of the counter along with a button to increment it. Step 1: Setting Up The StoreFirst things first, we'll use Redux to initialize our store and pass it down to our top-level container component. Redux provides a function, // ./src/index.js
import React from "react";
import ReactDOM from "react-dom";
import { createStore } from "redux"; /* code change */
import counterReducer from "./reducers/counterReducer.js";
import App from "./App";
import "./index.css";
const store = createStore(counterReducer); /* code change */
ReactDOM.render(<App />, document.getElementById("root")); Now, with the above set up, let's pass // ./src/index.js
import React from "react";
import ReactDOM from "react-dom";
import { createStore } from "redux";
import counterReducer from "./reducers/counterReducer.js";
import App from "./App";
import "./index.css";
const store = createStore(counterReducer);
ReactDOM.render(
<App store={store} /> /* code change */,
document.getElementById("root")
); So, to recap, just like we did previously, we call our createStore() method
in // ./src/reducers/counterReducer.js
export default function counterReducer(
state = {
clicks: 0
},
action
) {
switch (action.type) {
case "INCREASE_COUNT":
return {
clicks: state.clicks + 1
}
default:
return state;
}
} Each time an action with type 'INCREASE_COUNT' is dispatched to the reducer, the value of the counter is incremented. Instead of having all of our functions encapsulated in a closure within
Once we've created the store and passed it to the // ./src/App.js
import React, { Component } from "react";
import "./App.css";
class App extends Component {
handleOnClick = () => {
this.props.store.dispatch({
type: 'INCREASE_COUNT',
});
}
render() {
return (
<div className="App">
<button onClick={this.handleOnClick}>Click</button>
<p>{this.props.store.getState().clicks}</p>
</div>
);
}
}
export default App; As you recall, the store contains two methods: Now, with this code in place, if you boot up the app you should see a button on the page, followed by a zero. Then, if you click on the button... nothing happens. So what's gone wrong here? Well, we've done the work necessary to create the store and made it accessible to our app so the state is updated when we click the button, but we haven't yet done all the work necessary to get our React and Redux libraries communicating with each other properly so the page re-renders once the state is updated. We'll tackle that in the next lesson. In the meantime, how do we know our state is getting updated? Let's get some feedback so we can find out. Add Logging to Our ReducerFirst, let's log our action and the new state. So we'll change the reducer as follows: // ./src/reducers/counterReducer
export default function counterReducer(
state = {
clicks: 0
},
action
) {
console.log(action);
switch (action.type) {
case "INCREASE_COUNT":
console.log("Current state.clicks %s", state.clicks);
console.log("Updating state.clicks to %s", state.clicks + 1);
return {
clicks: state.clicks + 1
};
default:
console.log("Initial state.clicks: %s", state.clicks);
return state;
}
} Ok, so this may look like a lot, but really all we're doing is adding some logging behavior. At the top of the function, we are logging the action. After the case statement, we are logging our current state first, followed by the value the state is being updated to. Under the default case statement, we can just log the previous state because this state will be unchanged. Now, refresh your app, and give it a shot. When the page loads, you should see
the initial action being logged, along with the initial value of the counter, 0.
This is coming from our default case. Then, when you click the button, you
should see the Redux DevToolsThere is this amazing piece of software that allows us to nicely view the state of our store and each action that is dispatched. (In actuality, the software can do a lot more for us than that; you can read up on it here: redux-devtools-extension.) Ok, so let's get to incorporating the devtools. In fact, every time we use the Redux library going forward, we should make sure we incorporate devtools. Otherwise, you are flying blind. First, just Google for Redux Devtools Chrome. You should find the Chrome extension for Redux. Please download it, and refresh Chrome. To verify that you have successfully installed the extension, go to your developer console in Google Chrome (press command+shift+c to pull it up). In the top bar you will see a couple of arrows. Click those arrows, and if you see Redux in the dropdown, you have properly installed the Chrome extension. Step one is done. Second, we need to tell our application to communicate with this extension. Doing so is pretty easy. Let's change the arguments to our createStore method to the following: // ./src/index.js
import React from "react";
import ReactDOM from "react-dom";
import { createStore } from "redux";
import counterReducer from "./reducers/counterReducer";
import App from "./App";
import "./index.css";
const store = createStore(
counterReducer,
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
); /* code change */
ReactDOM.render(
<App store={store} />,
document.getElementById("root")
); Ok, notice that we are still passing through our reducer to the createStore
method. The second argument is accessing our browser to find a method called
Whew! SummaryIn this lesson, we saw how to use the createStore() method. We saw that we can rely on the Redux library to provide this method, and that we still need to write our own reducer to tell the store what the new state should be given a particular action. We saw that when using the createStore() method and passing through a reducer, we are able to change the state just as we did previously. We were able to see these changes by hooking our application up to a Chrome extension called Redux Devtools, and then providing the correct configuration. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论