In React, passing data between components can be achieved through props, context, or by using state management libraries like Redux or Recoil. Here are some common ways to pass data between components:
Props:
jsximport React from 'react';
import ChildComponent from './ChildComponent';
const ParentComponent = () => {
const data = "Hello from parent";
return (
<ChildComponent data={data} />
);
};
export default ParentComponent;
jsximport React from 'react';
const ChildComponent = (props) => {
return (
<div>{props.data}</div>
);
};
export default ChildComponent;
State:
jsximport React, { useState } from 'react';
import ChildComponent from './ChildComponent';
const ParentComponent = () => {
const [data, setData] = useState("Hello from parent");
return (
<ChildComponent data={data} />
);
};
export default ParentComponent;
jsximport React from 'react';
const ChildComponent = (props) => {
return (
<div>{props.data}</div>
);
};
export default ChildComponent;
Context:
jsximport React, { createContext, useContext } from 'react';
const DataContext = createContext();
export const DataProvider = ({ children }) => {
const data = "Hello from context";
return (
<DataContext.Provider value={data}>
{children}
</DataContext.Provider>
);
};
export const useData = () => useContext(DataContext);
jsximport React from 'react';
import { useData } from './DataContext';
const ChildComponent = () => {
const data = useData();
return (
<div>{data}</div>
);
};
export default ChildComponent;
DataProvider
at the top level:jsximport React from 'react';
import { DataProvider } from './DataContext';
import ChildComponent from './ChildComponent';
const App = () => {
return (
<DataProvider>
<ChildComponent />
</DataProvider>
);
};
export default App;
Redux:
redux
and react-redux
libraries:bashnpm install redux react-redux
jsx// actions.js
export const setData = (data) => ({
type: 'SET_DATA',
payload: data,
});
jsx// reducers.js
const dataReducer = (state = '', action) => {
switch (action.type) {
case 'SET_DATA':
return action.payload;
default:
return state;
}
};
export default dataReducer;
jsx// store.js
import { createStore } from 'redux';
import dataReducer from './reducers';
const store = createStore(dataReducer);
export default store;
jsx// index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import store from './store';
import App from './App';
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);
jsx// App.js
import React from 'react';
import { connect } from 'react-redux';
import { setData } from './actions';
const App = ({ data, setData }) => {
return (
<div>
<div>{data}</div>
<button onClick={() => setData("New data")}>Change Data</button>
</div>
);
};
const mapStateToProps = (state) => ({
data: state,
});
const mapDispatchToProps = {
setData,
};
export default connect(mapStateToProps, mapDispatchToProps)(App);
These are the basic ways to pass data between components in React. The choice of method depends on the size and structure of your application, as well as your specific requirements for state management.