Sure, I can help you with that! Let's start by creating a basic React project and installing some dependencies:
npx create-react-app my-app
cd my-app
npm install styled-components redux react-redux
Next, let's create a MyContext.js file to demonstrate how to use Context in React:
import React from 'react'
const MyContext = React.createContext()
export const MyProvider = (props) => {
const [count, setCount] = React.useState(0)
return (
<MyContext.Provider value={{ count, setCount }}>
{props.children}
</MyContext.Provider>
)
}
export default MyContext
In this file, we create a MyContext object using React.createContext() and define a MyProvider component that provides a count state variable and a setCount function to its children via the value prop of MyContext.Provider.
Now let's create a Counter.js component that uses useContext to consume the count and setCount values from our MyContext:
import React, { useContext } from 'react'
import MyContext from './MyContext'
const Counter = () => {
const { count, setCount } = useContext(MyContext)
return (
<div>
<h1>{count}</h1>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
)
}
export default Counter
In this component, we import MyContext and use the useContext hook to consume its values. We then render the current count value and a button that calls setCount to increment it when clicked.
Let's also create a TextInput.js component that uses useRef to store a reference to an input element and demonstrate how to use it:
import React, { useRef } from 'react'
const TextInput = () => {
const inputRef = useRef(null)
const handleFocus = () => {
inputRef.current.focus()
}
return (
<div>
<input ref={inputRef} />
<button onClick={handleFocus}>Focus Input</button>
</div>
)
}
export default TextInput
In this component, we create a inputRef object using useRef(null) and pass it to the ref prop of the input element. We then define a handleFocus function that uses the focus() method of inputRef.current to focus the input when the button is clicked.
Now let's create a Timer.js component that uses useEffect to set up a timer and demonstrate how to use it:
import React, { useState, useEffect } from 'react'
const Timer = () => {
const [time, setTime] = useState(0)
useEffect(() => {
const timer = setInterval(() => setTime(time + 1), 1000)
return () => clearInterval(timer)
}, [time])
return (
<div>
<h1>{time}</h1>
</div>
)
}
export default Timer
In this component, we create a time state variable using useState(0) and use useEffect to set up a timer that increments time every second. We also define a cleanup function that clears the timer when the component unmounts.
Finally, let's create a CounterButton.js component that uses Redux to manage a counter state and demonstrate how to use it:
import React from 'react'
import { connect } from 'react-redux'
import { incrementCounter } from './reducers/counter'
const CounterButton = ({ count, incrementCounter }) => {
return (
<div>
<h1>{count}</h1>
<button onClick={incrementCounter}>Increment</button>
</div>
)
}
const mapStateToProps = (state) => ({ count: state.counter.count })
const mapDispatchToProps = { incrementCounter }
export default connect(mapStateToProps, mapDispatchToProps)(CounterButton)
In this component, we use connect from react-redux to connect the component to the Redux store and map the count state value and incrementCounter action to the component's props. We then render the current count value and a button that calls the incrementCounter action when clicked.
Lastly, to demonstrate the use of styled-components, let's create a StyledComponents.js file and define a Box component:
import styled from 'styled-components'
const Box = styled.div`
background-color: ${({ color }) => color || 'gray'};
width: ${({ size }) => size || '100px'};
height: ${({ size }) => size || '100px'};
`
export default Box
In this file, we use the styled function from styled-components to define a Box component with background-color, width, and height properties that can be passed as props. We also use || to provide default values in case these props are not specified.
Now we can use all of these components in our App.js file to create a complete example:
import React from 'react'
import MyContext, { MyProvider } from './MyContext'
import Counter from './Counter'
import TextInput from './TextInput'
import Timer from './Timer'
import CounterButton from './CounterButton'
import Box from './StyledComponents'
const App = () => {
return (
<>
<MyProvider>
<Counter />
</MyProvider>
<TextInput />
<Timer />
<CounterButton />
<Box color='pink' size='150px' />
</>
)
}
export default App
In this file, we wrap the Counter component with the MyProvider component to provide the count and setCount values to it via MyContext. We also render the TextInput, Timer, CounterButton, and Box components to demonstrate the use of useRef, useEffect, Redux, and styled-components, respectively.
I hope this helps! Let me know if you have any questions.