Redux - 401-advanced-javascript-v/seattle-javascript-401d29 GitHub Wiki
As you develop React applications, you may find that keeping all your state in a top-level component is no longer sufficient for you. You may also have a lot of data changing in your application over time. Redux helps solve these kinds of problems. Mind you, it isn’t the only solution out there. For example, in larger apps with a lot of moving pieces, state management becomes a huge concern. Redux ticks that off quite well without performance concerns or trading off testability.
src/App.js
import React, { Component } from "react";
import HelloWorld from "./HelloWorld";
class App extends Component {
state = {
tech : "React"
}
render() {
return <HelloWorld tech={this.state.tech}/>
}
}
export default App;
Store
The Redux store will be responsible for managing the App’s state. With that being said, we need to remove the current state object from App/>.
import React, { Component } from "react";
import HelloWorld from "./HelloWorld";
class App extends Component {
// the state object has been removed.
render() {
return <HelloWorld tech={this.state.tech}/>
}
}
export default App;
Here’s what the code to create a Redux store looks like:
import { createStore } from "redux"; //an import from the redux library
const store = createStore(); // an incomplete solution - for now.
Reducer
For a Redux reducer, you always return the new state of your application initialState is the second argument passed into the createStore function call. App.js
import React, { Component } from "react";
import HelloWorld from "./HelloWorld";
import reducer from "./reducers";
import { createStore } from "redux";
const initialState = { tech: "React " };
const store = createStore(reducer, initialState);
class App extends Component {
render() {
return <HelloWorld tech={this.state.tech}/>
}
}
export default App;