Last active
April 11, 2020 12:48
-
-
Save madchester/2cc239fc57799f6e596dcdf2685dc042 to your computer and use it in GitHub Desktop.
sample redux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function getList() { | |
return (dispatch) => { | |
return axios.get( | |
`sampleurl` | |
headers: { | |
'Authorization': localdb.getItem('token') | |
} | |
}) | |
.then((reponseList) => { | |
dispatch({ | |
type: types.GET_LIST_SUCCESS, | |
payload: { | |
data: reponseList.data.data | |
} | |
}); | |
}) | |
.catch((sampleError) => { | |
throw(sampleError); | |
}) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Sample extends Component { | |
constructer(props){ | |
super(props) | |
this.state = { | |
list : [] | |
} | |
} | |
componentWillMount() { | |
this.props.sampleActions.getList() | |
} | |
componentWillReceiveProps(nextProps){ | |
if(nextProps.sample.list && nextProps.sample.list.length > 0){ | |
let y = [...nextProps.sample.list]; | |
this.setState({ | |
list: y | |
}) | |
} | |
} | |
render(){ | |
let x = [...this.state.list] | |
return( | |
<DataTable data={x} /> | |
) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function sampleReducer(state = initialState.sample, action) { | |
switch (action.type) { | |
case types.GET_LIST_SUCCESS: | |
return Object.assign({}, state, { | |
list: [...action.payload.data] | |
}); | |
default: | |
return state; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment