Skip to content

Instantly share code, notes, and snippets.

@madchester
Last active April 11, 2020 12:48
Show Gist options
  • Save madchester/2cc239fc57799f6e596dcdf2685dc042 to your computer and use it in GitHub Desktop.
Save madchester/2cc239fc57799f6e596dcdf2685dc042 to your computer and use it in GitHub Desktop.
sample redux
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);
})
}
}
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} />
)
}
}
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