Who needs Vanilla-js?
const Person = () => {
return <p>Max</p>
}
// or:
function Person() {
return <p>Max</p>
}
<Person/> // renders <p>Max</p>
function Person(props) {
return <p>{props.name}</p>
}
<Person name='Anna'/> // renders <p>Anna</p>
function Person(props) {
return <p>{props.name}, {props.age}</p>
}
<Person name='Anna' age={23} />
// renders <p>Anna, 23</p>
const OurComponent = () => {
return (
<div>
{6 > 5 ? (
<p>6 is greater</p>
) : (
<p>6 is less or equal</p>
)}
</div>
)
}
function App() {
return (
<div>
{!loading ? (
<p>Main content</p>
) : (
<p>loading...</p>
)}
</div>
)
}
const PersonComponent = (props) => {
return (
<p>
{props.name} is {props.age} years old
</p>
)
}
function App() {
const props = { name: 'Max', age: 23 }
return <PersonComponent {...props} />
}
const [count, changeCount] = useState(0)
count
// returns the current state '0'
changeCount(5)
// function, to change the state to '5'
const Person = (props) => {
return (
<div>
<p>Name: {props.name}</p>
<p>Age: {props.age}</p>
</div>
)
}
const Person = (props) => {
const { name, age } = props
return (
<div>
<p>Name: {name}</p>
<p>Age: {age}</p>
</div>
)
}
function hello() {
alert("hello")
}
function App() {
return <button onClick={hello}>
Click me
</button>
}
function App() {
return <button onClick={() => alert('hello')}>
Click me
</button>
}
function App() {
const [input, changeInput] = useState("")
function handleChange(event) {
changeInput(event.target.value)
}
return (
<form onSubmit={() => alert(input)}>
<input type="text" value={input} onChange={handleChange}/>
<input type="submit" value="Submit"/>
</form>
)
}
function App() {
const [count, setCount] = useState(0)
return (
<div>
<p>The count: {count}</p>
<button onClick={() => setCount(count + 1)}>
Increment
</button>
</div>
)
}
function App() {
useEffect(() => {
console.log("component just rendered")
}, [])
return (
<div>
<p>Hello World!</p>
</div>
)
}
function App() {
const [count, setCount] = useState(0)
useEffect(() => {
console.log("count changed")
}, [count])
return (
<div>
<p>The count: {count}</p>
<button onClick={() => setCount(count + 1)}>
Increment
</button>
</div>
)
}
function Status() {
useEffect(() => {
console.log("mounted")
return () => {
console.log("unmounted)
}
})
return <p>I am here! </p>
}
function App() {
const [visible, setVisible] = useState(false)
return (
<div>
<p>Optional component:</p>
{visible && <Status />}
<button onClick={() => setVisible(!visible)}>
Toggle visibility of Status
</button>
</div>
)
}
function App() {
const elementRef = useRef()
useEffect(() => {
const divElement = elementRef.current
console.log(divElement)
// logs <div>A random div</div>
}, [])
return (
<div ref={elementRef} id="something">
A random div
</div>
)
}
function App() {
const [count, setCount] = useState(10)
const expensiveComputation = useMemo(() => {
return count * 2
}, [count])
return (
<>
<p>Count: {count}</p>
<p>Expensive count: {expensiveComputation}</p>
<button onClick={() => setCount(count + 1)}>Increment count</button>
</>
)
}
function App() {
function reducer(state, action) {
switch (action.type) {
case 'plus':
return state + 1
case 'minus':
return state - 1
}
}
const [count, dispatch] = useReducer(reducer, 0)
return (
<>
Count: {count}
<button onClick={() => dispatch({ type: 'plus' })}>Plus 1</button>
<button onClick={() => dispatch({ type: 'minus' })}>Minus 1</button>
</>
)
}
function App() {
const [count, setCount] = useState(10)
const doubleCount = useCallback(() => {
return count * 2
}, [count])
return <ChildComponent getValue={doubleCount} />
}