Skip to content

Instantly share code, notes, and snippets.

View aibolik's full-sized avatar
🇰🇿
Developer from KZ

Aibol Kussain aibolik

🇰🇿
Developer from KZ
View GitHub Profile
const ToastContext = React.createContext(null);
let id = 1;
const ToastProvider = ({ children }) => {
const [toasts, setToasts] = useState([]);
const addToast = useCallback(content => {
setToasts(toasts => [
...toasts,
const ToastProvider = ({ children }) => {
// ...
const addToast = useCallback(content => {
setToasts(toasts => [
...toasts,
{ id: id++, content }
]);
}, [setToasts]);
@aibolik
aibolik / ToastProvider-addToast.jsx
Created January 12, 2020 21:09
addToast function in ToastProvider component(part of posts https://aibolik.github.io/blog/creating-toast-api-with-react-hooks)
let id = 0;
const ToastProvider = ({ children }) => {
// ...
const addToast = useCallback(content => {
setToasts(toasts => [
...toasts,
{ id: id++, content }
]);
}, [setToasts]);
const ToastProvider = ({ children }) => {
const [toasts, setToasts] = useState([]);
// ...
}
const Wrapper = styled.div`
position: absolute;
/* Top right corner */
right: 0;
top: 0;
`;
const ToastContainer = ({ toasts }) => {
return createPortal(
<Wrapper>
const Wrapper = styled.div`
margin-right: 16px;
margin-top: 16px;
width: 200px;
position: relative;
padding: 16px;
border: 1px solid #d7d7d7;
border-radius: 3px;
background: white;
@aibolik
aibolik / replace-string.js
Created August 28, 2019 05:02
Wrap a term in string with some other string(like HTML tag `strong`)
let title = title.replace(new RegExp(search, 'ig'), `<strong>$&</strong>`);
@aibolik
aibolik / HttpToHttps.js
Created September 5, 2018 19:20
Force all requests on production to be served over https
app.use(function (req, res, next) {
if (req.headers['x-forwarded-proto'] !== 'https' && process.env.NODE_ENV === 'production') {
const secureUrl = 'https://' + req.hostname + req.originalUrl
res.redirect(302, secureUrl)
}
next()
})
@aibolik
aibolik / EndlessRecyclerViewScrollListener.java
Created August 4, 2017 04:57
Scroll listener for RecyclerView that handles pagination
public abstract class EndlessRecyclerViewScrollListener extends RecyclerView.OnScrollListener {
// The minimum amount of items to have below your current scroll position
// before loading more.
private int visibleThreshold = 5;
// The current offset index of data you have loaded
private int currentPage = 1;
// The total number of items in the dataset after the last load
private int previousTotalItemCount = 0;
// True if we are still waiting for the last set of data to load.
private boolean loading = true;
class StoreManager extends React.Component {
constructor(props){
super(props)
}
componentWillReceiveProps(newProps){
if(newProps.hash !== this.props.hash){
newProps.onUpdate()
}
}