Skip to content

Mobx tutorial changes from the video #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: mobx_tutorial
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/store/superheroes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {action, computed, makeObservable, observable} from 'mobx';

class Superheroes {
superheroes = [];

constructor() {
makeObservable(this, {
superheroes: observable,
addHero: action,
deleteHero: action,
count: computed,
});
}
addHero(hero) {
console.log('* ADD HERO: ' + JSON.stringify({...hero, id: Math.random()}));
this.superheroes = [...this.superheroes, {...hero, id: Math.random()}];
}

deleteHero(id) {
this.superheroes = this.superheroes.filter((hero) => hero.id !== id);
}
get count() {
return this.superheroes.length;
}
}

export const superheroStore = new Superheroes();
62 changes: 26 additions & 36 deletions src/view/ListScreen.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,38 +1,28 @@
import React, { useEffect, useState } from 'react';
import React from 'react';
import {observer} from 'mobx-react';

import { CheeatahListText } from '../style/text';
import { Container } from '../style/container';
import { FlatList } from 'react-native';
import { ListSeparator } from '../style/separator';
import {CheeatahListText} from '../style/text';
import {Container} from '../style/container';
import {FlatList} from 'react-native';
import {ListSeparator} from '../style/separator';
import {superheroStore} from '../store/superheroes';

export default ListScreen = ({ route }) => {
const [superheroList, setHeroList] = useState([]);

useEffect(() => {
setHeroList(route.params.list)
}, [])


deleteHero = (id) => {
let filteredHeroes = superheroList.filter(hero => hero.id !== id)
setHeroList(filteredHeroes)
}

return (
<Container>
<FlatList
data={superheroList}
keyExtractor={(item) => item.id}
style={{ width: '100%', marginTop: 20, paddingBottom: 10 }}
ItemSeparatorComponent={() => <ListSeparator />}
renderItem={({ item }) => (
<CheeatahListText
content={`Name: ${item.name}`}
onLongPress={() => deleteHero(item.id)}
description={`Power: ${item.power}`}
/>
)}
/>
</Container>
);
}
export default ListScreen = observer(() => {
return (
<Container>
<FlatList
data={superheroStore.superheroes}
keyExtractor={(item) => item.id}
style={{width: '100%', marginTop: 20, paddingBottom: 10}}
ItemSeparatorComponent={() => <ListSeparator />}
renderItem={({item}) => (
<CheeatahListText
content={`Name: ${item.name}`}
onLongPress={() => superheroStore.deleteHero(item.id)}
description={`Power: ${item.power}`}
/>
)}
/>
</Container>
);
});
93 changes: 54 additions & 39 deletions src/view/home.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,44 +1,59 @@
import React, { useState } from 'react';
import React, {useState} from 'react';
import {FlatList} from 'react-native';
import {Observer} from 'mobx-react';

import { Title } from '../style/text';
import { Container, RowContainer } from '../style/container';
import { CheetahInput } from '../style/input';
import { CheeatahButton } from '../style/button';
import {superheroStore} from '../store/superheroes';
import {CheeatahListText} from '../style/text';
import {ListSeparator} from '../style/separator';
import {Title} from '../style/text';
import {Container, RowContainer} from '../style/container';
import {CheetahInput} from '../style/input';
import {CheeatahButton} from '../style/button';

export default Home = ({ navigation }) => {
export default Home = ({navigation}) => {
const [name, setName] = useState('');
const [power, setPower] = useState('');

const [superheroList, setHeroList] = useState([]);
const [name, setName] = useState('');
const [power, setPower] = useState('');
console.log('* reloading screen...');

addHero = () => {
setHeroList(list => [...list, { name, power, id: Math.random() }])
}
addHero = () => {
setHeroList((list) => [...list, {name, power, id: Math.random()}]);
};

return (
<Container>
<Title>Superheroes</Title>
<CheetahInput
hint='Name'
onChangeText={text => setName(text)}
/>
<CheetahInput
hint='Power'
onChangeText={text => setPower(text)}
/>
<RowContainer>
<CheeatahButton
title="Add"
onPress={(text) => addHero(text)}
marginRight={12}
/>
<CheeatahButton
title="View List"
onPress={() => navigation.navigate('List', {
list: superheroList,
})}
/>
</RowContainer>
</Container>
);
}
return (
<Container>
<Title>Superheroes</Title>
<CheetahInput hint="Name" onChangeText={(text) => setName(text)} />
<CheetahInput hint="Power" onChangeText={(text) => setPower(text)} />
<RowContainer>
<CheeatahButton
title="Add"
onPress={(text) => superheroStore.addHero({name, power})}
marginRight={12}
/>
<CheeatahButton
title="View List"
onPress={() => navigation.navigate('List')}
/>
</RowContainer>

<Observer>
{() => (
<FlatList
data={superheroStore.superheroes}
keyExtractor={(item) => item.id}
style={{width: '100%', marginTop: 20, paddingBottom: 10}}
ItemSeparatorComponent={() => <ListSeparator />}
renderItem={({item}) => (
<CheeatahListText
content={`Name: ${item.name}`}
onLongPress={() => superheroStore.deleteHero(item.id)}
description={`Power: ${item.power}`}
/>
)}
/>
)}
</Observer>
</Container>
);
};