Redux

Redux is a state management library
1 Create Initial State
2 Define Action Types
3 Define Action Creators
4 Create Reducers
5 Change the initial State
6 Pass the parameters to the Creator function and the reducers

Code
// Initial State

const initialState = {
  todos: [
    {
      text: "eat food",
    },
    {
      text: "Exercise",
    },
  ],
};

// Action Types
// create a simple action type

const ADD_TODO = "ADD_TODO";

// ACtion creators

function addTodo(text) {
  return {
    type: ADD_TODO,
    payload: text,
  };
}

//create reducers
function todoReducer(state = [], action) {
  switch (action.type) {
    case ADD_TODO:
      return [...state.todos, { text: action.payload }];
    default:
      return state;
  }
}

console.log("Initial State : ", initialState);

// Lets make changes to initial states
const action = addTodo("Make it work");
const newState = todoReducer(initialState, action);

console.log(newState);

0
0
Rubixphys12 15320 points

                                    Use Redux tool kit

0
0
3.67
3
Awgiedawgie 440215 points

                                    npm install redux // adding redux to the project
// creating types 
export const SET_USER = 'SET_USER'
//creating actions 
export const setUser = user => {
  return {
    type : SET_USER,
    payload : {
      currentUser : user
    }
  }
// creating reducers 
  const user_reducer = (state=intialState,action)=>{
    switch(action.type){
      case SET_USER :
        return {
          currentUser : action.payload.currentUser
        }
    }

3.67 (3 Votes)
0
0
0
Awgiedawgie 440215 points

                                    npm install redux // adding redux to the project
// creating types 
export const SET_USER = 'SET_USER'
//creating actions 
export const setUser = user => {
  return {
    type : SET_USER,
    payload : {
      currentUser : user
    }
  }
// creating reducers 
  const user_reducer = (state=intialState,action)=>{
    switch(action.type){
      case SET_USER :
        return {
          currentUser : action.payload.currentUser
        }
    }

0
0
0
0
Phoenix Logan 186120 points

                                    > Redux is an open-source JavaScript library for managing application state.
> It is most commonly used with libraries such as React or Angular for 
building user interfaces.

0
0
3.86
7
Awgiedawgie 440215 points

                                    // component.js
showNotificationWithTimeout(this.props.dispatch, 'You just logged in.')

// otherComponent.js
showNotificationWithTimeout(this.props.dispatch, 'You just logged out.')  
复制代码

3.86 (7 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source