rdx Flashcards

1
Q

redux: to write a reducer, type

A

in reducers/classNameReducer.js

import { FETCH_CLASS_NAME, CREATE_CLASS_NAME } from ‘../actions/types.js’

const initialState = {
items: [],
items2: []
}

export default function (state = initialState, action) {
switch (action.type) {
case FETCH_CLASS_NAME:
return {…state, items: action.payload} // spread prior state, then overwrite the key
case CREATE_CLASS_NAME:
return {…state, items2: [{id:30, title: ‘string’}, …state.items]}
default:
return state
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

redux: To map parts of the state that come in from your reducer to this.props,

and to map action functions to this.props, type

A

import {connect} from ‘react-redux’

const mapStateToProps = (state, props) => ({
propName: state.nameInRootReducer.items,
propName2: state.nameInRootReducer.items2
})

const mapDispatchToProps = {
actionFunc1: thunkFunc1,
actionFunc2: actionFunc2
}

export default connect(mapStateToProps, mapDispatchToProps)(componenName);

remember:
not export default function

notes:
bindActionCreators in mapDispatchToProps is optional, and if you just use an object literal it happen automatically.
https://blog.isquaredsoftware.com/2016/10/idiomatic-redux-why-use-action-creators/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

redux: To create a thunk action creator, type

A

in actions/classNameActions.js

import { FETCH_CLASS_NAME, CREATE_CLASS_NAME } from ‘./types’

note: This is a thunk, not an action creator

export const fetchClassName = () => (dispatch) => {
  fetch('http://url.com/')   
  .then(response => response.json())
  .then(data => dispatch({
    type: FETCH_POSTS,
    payload: data // dispatch is like pub sub, i can send data to any reducer from here
    }))
}

note: dispatch hes to be called in the thunk creator because that where the promise will resolve asynchronously.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

promises are necessary when you are

A

relying on the return value of a function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

To return a new object that combines multiple objects into one, type

A

Object.assign({}, {key: ‘string’}, {key2: ‘string2’})
or
{…{}, …{key: ‘string’}, …{key2: ‘string2’}}

Note: Repeated names will be overwritten by the later name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The two ways to define a method in an object literal are

A
{ 
  key: "string",
  funcName() {
    return "Luke"
  },
}
Note: still requires commas
or
{ 
  key: "string",
  funcName: function() {
    return "Luke"
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

To be able to call a function using objectName.funcName = “string” and have the object you put after the = be used as the parameter, type

A
{
  set funcName(param) {
    this.privateVarName.push(param);
  },
  privateVarName: []
}

Note: Private vars are not accessible from instances because they don’t use this.varName

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

the get and set keywords do work in

A

object literals, as well as constructor functions and classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

To create a getter function that can be called without using (), type

A
{
  get funcName() { 
    return "string"
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

To generate a class based component with all lifecycle methods in vscode, type

A

rcfc tab

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

To generate a functional component in vscode, type

A

rscp

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Only if you do not set mapDispatchToProps, you can use

A

this.props.dispatch({type: ‘INCREMENT’})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

redux automatically adds

A

as this.props.dispatch() in your component if you do not set mapDispatchToProps()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

an action is

A

an object with type and payload

{type : “ADD_TODO”, payload : “string”}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

converting an actionCreator into a closure is only necessary if

A

you need to make an async call before dispatching.

Note: You can dispatch before and after too.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

thunk: converting an actionCreator into a closure is only necessary if

A

you need to make an async call before dispatching.

Note: You can dispatch before and after too.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

best redux tutorial, aside from calling mapDispatchToProps mapActionsToProps, is

A

https://www.youtube.com/watch?v=OSSpVLpuVWA

and potentially for saying bindActionCreators is necessary

18
Q

use this for the redux chrome dev tools

A

https://youtu.be/OSSpVLpuVWA?t=1355

19
Q

rn: you should not use this.setState() in

A

componentDidMount

20
Q

to make initial api request for data, use

A

componentDidMount

21
Q

a thunk action can

A

dispatch an actionCreator from above it in the actions file.

22
Q

need to learn the proptype shortcuts

A

rpt

pta
ptb
ptr
ptn
pto
pts

https://marketplace.visualstudio.com/items?itemName=sledsworth.react-redux-es6-snippets

23
Q

The proptype shortcuts are

A

rpt to create main, then

pta
ptb
ptf
ptn
pto
pts

note: add r to end for required
https: //marketplace.visualstudio.com/items?itemName=sledsworth.react-redux-es6-snippets

24
Q

React: To add a key to a list that is being mapped into JSX, type

A

const itemArray = [‘string1’, ‘string2’, ‘string3’];

const myJxsArray = itemArray.map((item, key) =>
  -li key={key}>{item}-/li>
);
25
Q

redux thunk knows when you are using a synchronous actionCreator vs an asynchronous one because

A

you are passing a function that returns a function into the dispatch function, instead of an object or a function that returns an object, memorize media queries

26
Q

to install redux, type

A

npm install redux react-redux redux-thunk –save

27
Q

make sure to export

A

the constants in types.js

28
Q

the two ways to write mapDispatchToProps are

A

const mapDispatchToProps = (dispatch) => ({
fetchClassName: () => dispatch(fetchClassName())
})
note: this works exactly the same for thunk creators and action creators.
or
import { bindActionCreators } from ‘redux’
const mapDispatchToProps = (dispatch) => bindActionCreators({
fetchClassName: fetchClassName,
}, dispatch)
or
const mapDispatchToProps = {
fetchClassName: fetchClassName,
}
using an object like this is recommended

29
Q

react: The attributes input fields should have are

A

onChange={this.handleChange} value={this.state.inputField}

30
Q

the redux workflow is

A

add the event handler and make up the name
onClick={this.props.handleInputClick}

then add that name to mapDispatchToProps
const mapDispatchToProps = {
 handleInputClick: handleInputClick,
}

then import it from actions. If it is effecting a model different model create a new name to put before Actions
import {handleInputClick} from ‘./actions/emailActions’

create the type in types
export const HANDLE_INPUT_CLICK = "HANDLE_INPUT_CLICK"

Create or use a reducer that updates state

Add new reducers to root reducer

Add to mapSateToProps to access the state change.

31
Q

need to memorize how to import and use custom components, memorize how to create a basic component, learn how to make a navbar in flexbox

A

structure should be in components, display and container, push pop, shift, ushift, replace(), find, slice(), dict.keys(), Array(5), learn all the loops off by heart, and fo similar for python range, slice, len, split an array being looped into its parts in python and JS. print(‘’, ‘’) prints both with a space in between. Learn the python built in functions, learn how to add defaults to python function, and js function, learn global in python, I def need to memorize random for python and js.

32
Q

components are named in

A

CapitalCase

33
Q

To create a basic component, type

A

ComponentName.js

import React, { Component } from ‘react’;
import PropTypes from ‘prop-types’;

class ComponentName extends Component {
  constructor(props) {
    super(props);
  }
  render() {
    return ...
  }
}

ComponentName.propTypes = {

};

export default ComponentName;

34
Q

components file extension is

A

.js

35
Q

To import a custom component, type

A

import ComponentName from ‘./components/ComponentName.js’

-ComponentName />

36
Q

To make a fetch request of just JSON, type

A
fetch("url.com", {
    method: "POST",
    body: JSON.stringify({key: 'value'})
    headers: new Headers({
      'Accept': 'application/json, text/plain, */*',
      'Content-Type': 'application/json'
      'Authorization': 'Token ' + authKey
    })
})
37
Q

The three ways render components on a condition are

A

{ x > 10 andand -ComponentName /> }

{ false || -ComponentName /> }

{ x > 10 ? -ComponentName /> : -ComponentName2 /> }

38
Q

function creation workflow

A

1) name the function (should do one thing, start with verb)
2) use pseudocode to write steps
3) declare the vars you will use at top of function
4) If it needs to do 2 things, create a second function and trigger it at the bottom.

39
Q

the json rules are

A

keys must use double quotes
no trailing commas
white space doesnt matter

40
Q

an action creator is just a function that return an object

A

function that return an object, and that object is called an action, but is just {‘type’: TYPE_NAME, dataKey: ‘value’}

41
Q

A context is essentially

A

a container component but if you add variables to the context, they are available for all children components automatically.

42
Q

To make a functional component only render once, tyoe

A

React.memo around it

React.memo(function ContainerComponent() {
  return (
    -div />
  );
})