Skip to content

Instantly share code, notes, and snippets.

🏋
At the gym

Ferran Negre ferrannp

🏋
At the gym
Block or report user

Report or block ferrannp

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View react-native-folder-structure.md

React Native Folder Structure

Motivations

  • Sharing what has worked for me in different React Native projects
  • Reusing screens in different parts of your app
  • Easy to work separately in a feature
  • Add an extra level (nested folder) only when necessary
  • Don't overuse index.js for everything
View locale-android.java
static String currentLocale;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MainActivity.currentLocale = getResources().getConfiguration().locale.toString();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
View enhance-dropdown.js
const EnhanceDropdown = ComposedComponent => class extends Component {
constructor() {
super();
this.state = { isOpen: false };
this.onToggle = this.onToggle.bind(this);
this.handleDocumentClick = this.handleDocumentClick.bind(this);
this.onSelect = this.onSelect.bind(this);
}
componentDidMount() {
window.addEventListener('click', this.handleDocumentClick)
View stateles-dropdown.js
const Dropdown = (props) => (
<div className='dropdown'>
<button onClick={props.onToggle}>
Selected option: {props.data[props.optionSelected]}
</button>
<ul className={props.isOpen ? 'active':null}>
{
props.data.map((item, i) => {
return (
<li key={i} className={i === props.optionSelected ? 'selected':null}
View super-simple-cache-jest-test.js
it('does check if we already fetched that id and only calls fetch if necessary', () => {
const store = mockStore({id: 1234, isFetching: false }});
window.fetch = jest.fn().mockImplementation(() => Promise.resolve());
store.dispatch(fetchData(1234)); // Same id
expect(window.fetch).not.toBeCalled();
store.dispatch(fetchData(1234 + 1)); // Different id
expect(window.fetch).toBeCalled();
});
View fetch-error-jest-test.js
pit('calls request and failure actions if the fetch response was not successful', () => {
window.fetch = jest.fn().mockImplementation(() => Promise.resolve(mockResponse(
400, 'Test Error', '{"status":400, "statusText": Test Error!}')));
return store.dispatch(fetchData(1234))
.then(() => {
const expectedActions = store.getActions();
expect(expectedActions.length).toBe(2);
expect(expectedActions[0]).toEqual({type: types.FETCH_DATA_REQUEST});
View fetch-success-jest-test.js
pit('calls request and success actions if the fetch response was successful', () => {
window.fetch = jest.fn().mockImplementation(() =>
Promise.resolve(mockResponse(200, null, '{"id":"1234"}')));
return store.dispatch(fetchData(1234))
.then(() => {
const expectedActions = store.getActions();
expect(expectedActions.length).toBe(2);
expect(expectedActions[0]).toEqual({type: types.FETCH_DATA_REQUEST});
View async-action-fetch-data.js
import 'whatwg-fetch';
...
export function fetchData(id) {
return (dispatch, getState) => {
if(getState().id === id)) {
return; // No need to fetch
}
dispatch(requestData(id));
You can’t perform that action at this time.