Skip to content

Instantly share code, notes, and snippets.

function brokenReducer(state = initialState, action) {
switch(action.type) {
case 'INCREMENT':
// NO! BAD: this is changing state!
state.count++;
return state;
case 'DECREMENT':
// NO! BAD: this is changing state too!
state.count--;
import React from 'react';
import { connect } from 'react-redux';
class Counter extends React.Component {
increment = () => {
this.props.dispatch({ type: 'INCREMENT' });
}
decrement = () => {
this.props.dispatch({ type: 'DECREMENT' });
@dceddia
dceddia / App.js
Created June 15, 2018 02:03
Render the Camera, or the last photo.
render() {
const { photo } = this.state;
return (
<View style={{ flex: 1, width: '100%' }}>
{photo ? (
<ImageBackground
style={{ flex: 1 }}
source={{ uri: photo.uri }} />
) : (
@dceddia
dceddia / App.js
Created June 15, 2018 02:03
Using Autoshoot inside App
render() {
const { cameraPermission } = this.state;
// Render one of 3 things depending on permissions
return (
<View style={styles.container}>
{cameraPermission === null ? (
<Text>Waiting for permission...</Text>
) : cameraPermission === false ? (
<Text>Permission denied</Text>
@dceddia
dceddia / App.js
Created June 15, 2018 02:03
Importing Camera and rendering based on permissions.
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
// add this:
import { Camera, Permissions } from 'expo';
export default class App extends React.Component {
// initialize state
state = {
cameraPermission: null
@dceddia
dceddia / App.js
Created June 15, 2018 02:03
Upload each picture as it is taken
// Here's the timer code, lifted from takePicture:
queuePhoto = () => {
// In 27 seconds, turn the camera back on
setTimeout(() => {
this.setState({ photo: null });
}, PHOTO_INTERVAL - FOCUS_TIME);
// In 30 seconds, take the next picture
setTimeout(this.takePicture, PHOTO_INTERVAL);
}
@dceddia
dceddia / App.js
Created June 15, 2018 02:03
Add the takePicture method
takePicture = () => {
this.camera.takePictureAsync({
quality: 0.1,
base64: true,
exif: false
}).then(photo => {
this.setState({ photo });
})
}
@dceddia
dceddia / App.js
Created June 15, 2018 02:03
Expo-generated App.js file
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<Text>Open up App.js to start working on your app!</Text>
</View>
);
@dceddia
dceddia / App.js
Created June 15, 2018 02:03
Creating the Autoshoot component to render the Camera.
class Autoshoot extends React.Component {
render() {
return (
<View style={{ flex: 1, width: '100%' }}>
<Camera
style={{ flex: 1 }}
type={Camera.Constants.Type.back}
ref={cam => this.camera = cam}>
</Camera>
</View>
@dceddia
dceddia / App.js
Created June 15, 2018 02:03
Initialize Autoshoot's state
class Autoshoot extends React.Component {
state = {
photo: null
}
...
}