Bye Bye Prof

Théorie

Aide-mémoire

EXPO

JSX

Introduction au JSX

// Exemple simple de JSX
const Welcome = () => {
  return (
    <View>
      <Text>Bienvenue sur mon app!</Text>
    </View>
  );
};

Expressions en JSX

const Greeting = () => {
  const name = "Alice";
  const isLoggedIn = true;
  
  return (
    <View>
      <Text>Bonjour {name}!</Text>
      {isLoggedIn && <Text>Vous êtes connecté</Text>}
    </View>
  );
};

Styles et attributs en JSX

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    padding: 20,
  },
  text: {
    fontSize: 18,
    color: '#333',
  }
});

const StyledComponent = () => (
  <View style={styles.container}>
    <Text style={styles.text}>Texte stylé</Text>
  </View>
);

Components

Bases des Components

// Composant fonctionnel simple
const Header = () => {
  return (
    <View>
      <Text>Mon Application</Text>
    </View>
  );
};

Composition de Components

const Card = () => (
  <View style={styles.card}>
    <Header />
    <Content />
    <Footer />
  </View>
);

const Content = () => (
  <View>
    <Text>Contenu principal</Text>
  </View>
);

Cycle de vie des Components

const LifecycleComponent = () => {
  useEffect(() => {
    console.log('Component monté');
    return () => {
      console.log('Component démonté');
    };
  }, []);

  return <Text>Composant avec cycle de vie</Text>;
};

Props

Introduction aux Props

// Parent
const App = () => (
  <UserCard 
    name="Pierre"
    age={25}
    role="admin"
  />
);

// Enfant
const UserCard = (props) => (
  <View>
    <Text>Nom: {props.name}</Text>
    <Text>Age: {props.age}</Text>
    <Text>Rôle: {props.role}</Text>
  </View>
);

Props avancées

const Profile = ({ name, age = 20 }) => (
  <View>
    <Text>{name} ({age} ans)</Text>
  </View>
);

Profile.propTypes = {
  name: PropTypes.string.isRequired,
  age: PropTypes.number
};

Children Props

const Container = ({ children, title }) => (
  <View>
    <Text style={styles.title}>{title}</Text>
    {children}
  </View>
);

// Utilisation
const App = () => (
  <Container title="Mon conteneur">
    <Text>Contenu enfant</Text>
    <Button title="Cliquez-moi" />
  </Container>
);

State

Bases du State

const Counter = () => {
  const [count, setCount] = useState(0);
  
  return (
    <View>
      <Text>Compteur: {count}</Text>
      <Button 
        title="+"
        onPress={() => setCount(count + 1)}
      />
    </View>
  );
};

State complexe

const TodoList = () => {
  const [todos, setTodos] = useState([]);
  
  const addTodo = (text) => {
    setTodos([
      ...todos,
      { id: Date.now(), text, completed: false }
    ]);
  };
  
  return (
    <View>
      <Button title="Ajouter" onPress={() => addTodo("Nouvelle tâche")} />
      {todos.map(todo => (
        <Text key={todo.id}>{todo.text}</Text>
      ))}
    </View>
  );
};

State et Effects

const Timer = () => {
  const [seconds, setSeconds] = useState(0);
  
  useEffect(() => {
    const interval = setInterval(() => {
      setSeconds(s => s + 1);
    }, 1000);
    
    return () => clearInterval(interval);
  }, []);
  
  return <Text>Temps écoulé: {seconds}s</Text>;
};