Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"dependencies": {
"react": "^16.10.2",
"react-dom": "^16.10.2",
"react-scripts": "3.2.0"
"react-redux": "^7.1.1",
"react-scripts": "3.2.0",
"redux": "^4.0.4"
},
"scripts": {
"start": "react-scripts start",
Expand Down
42 changes: 26 additions & 16 deletions src/containers/container1.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
import React, {Component} from 'react';

class Cotainer1 extends Component {
Arr1 = [
{id:1, text: 'text 1', number: 1},
{id:2, text: 'text 2', number: 2},
{id:3, text: 'text 3', number: 3},
{id:4, text: 'text 4', number: 4},
{id:5, text: 'text 5', number: 5}
]
import * as ACTION_TYPES from '../store/actions/actions_types'
import * as ACTIONS from '../store/actions/actions'

renderListItem = (props) => (
<div>
{props.item.text}
<p>{props.item.number}</p>
</div>
)
import { connect } from 'react-redux'

class Cotainer1 extends Component {

render(){
return(
<div>
{this.Arr1.map((item, index) => (<this.renderListItem key={item.id} item={item} />))}
<button onClick={() => console.log(this.props.stateprop1)}> GetState</button>
<button onClick={() => this.props.action1()}> Dispatch Action 1</button>
<button onClick={() => this.props.action1()}> Dispatch Action 2</button>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can define a var instead of using the same string

<button onClick={() => this.props.action_creator1()}> Dispatch Action Creator 1</button>
<button onClick={() => this.props.action_creator2()}> Dispatch Action Creator 2</button>
</div>
)
}
}

export default Cotainer1
function mapStateToProps(state) {
return {
stateprop1: state.stateprop1
}
}

function mapDispatchToProps(dispatch) {
return {
action1: () => dispatch(ACTIONS.SUCCESS),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a better naming for the functions

action2: () => dispatch(ACTIONS.FAILURE),
action_creator1: () => dispatch(ACTIONS.success()),
action_creator2: () => dispatch(ACTIONS.failure())
}
}

export default connect(mapStateToProps, mapDispatchToProps) (Cotainer1)
11 changes: 10 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,14 @@ import React from 'react';
import ReactDOM from 'react-dom';
import App from './App'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove empty line

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not done yet

ReactDOM.render(<App />, document.getElementById('root'));
import { Provider } from 'react-redux';
import rootReducer from './store/reducers/reducer1';
import { createStore } from 'redux';

let store = createStore(rootReducer)

ReactDOM.render(<Provider store={store}>
<App />
</Provider>,
document.getElementById('root'));

21 changes: 21 additions & 0 deletions src/store/actions/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as ACTION_TYPES from './actions_types'

export const SUCCESS = {
type: ACTION_TYPES.SUCCESS
}

export const FAILURE = {
type: ACTION_TYPES.FAILURE
}

export const success = () => {
return {
type: ACTION_TYPES.SUCCESS
}
}

export const failure = () => {
return {
type: ACTION_TYPES.FAILURE
}
}
3 changes: 3 additions & 0 deletions src/store/actions/actions_types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const SUCCESS = "SUCCESS"

export const FAILURE = "FAILURE"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add empty line at the end of the file

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not done yet

24 changes: 24 additions & 0 deletions src/store/reducers/reducer1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as ACTION_TYPES from '../actions/actions_types'

const initialState = {
stateprop1: false
}

const rootReducer = (state = initialState, action) => {
switch(action.type){
case ACTION_TYPES.SUCCESS:
return {
...state,
stateprop1: true
}
case ACTION_TYPES.FAILURE:
return {
...state,
stateprop1: false
}
default:
return state
}
}

export default rootReducer