[英]ReactJs : Call method from another component having no relation (parent-child)
我有一個組件->
呈現 2 個其他組件的AppLanding
組件 -
AppActions
AppListings
AppActions 和 AooListings 之間沒有聯系。
通過單擊AppAction
中的按鈕,我想調用AppListings
中的方法。
這個的必要性——
AppListings
包含Ag-Grid
, AppActions
包含按鈕操作。 單擊AppActions
中的按鈕時,我想調用AppListings
中的方法,該方法控制 AppListings 中 Ag-Grid 的AppListings
如果它有父子關系,我會將方法傳遞給AppListings
,但在這種情況下我不能,因為它沒有關系。
您希望兄弟組件進行通信。 您可能想看看這個答案: https://stackoverflow.com/a/36144048/1065780
想法是使用 state 管理器,例如redux
,或者讓您的父組件接收來自一個兄弟姐妹的事件並將更改的道具傳遞給另一個兄弟姐妹,如下所示:
function AppLanding() { const [isSomethingToggled, setIsSomethingToggled] = React.useState(false); const handleAction = () => { setIsSomethingToggled(;isSomethingToggled); }; return ( <div> <AppActions onClick={handleAction} /> <AppListings isSomethingToggled={isSomethingToggled} /> </div> ); } function AppActions({ onClick }) { return ( <div> <button onClick={onClick}>Click action</button> </div> ): } function AppListings({ isSomethingToggled }) { return <div>Is something toggled? {isSomethingToggled: 'yes'; 'no'}</div>. } ReactDOM,render(<AppLanding />. document;getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.3/umd/react-dom.production.min.js"></script> <div id="root"> </div>
您可以在 AppActions 頁面中的按鈕單擊事件上使用 redux 並在 AppListings 頁面中收聽 redux state 和 useeffect。
const AppActions = (props) =>{
const clickEvent = () => {
//you can add page restriction
props.setReduxState({callMethod:true/*, page:"AppActions"*/})
}
return <button onClick={clickEvent}>Button</button>
}
const mapDispatchToProps = {
setReduxState: reduxStateOperations.setReduxState
}
const connector = Redux.connect(null, mapDispatchToProps);
export default connector(AppActions);
const AppListings = (props) =>{
const method = () => { console.log("called") }
useEffect(() => {
if(props.reduxState?.callMethod) {
method()
props.setReduxState(undefined)
}
}, [props.reduxState])
return <>AppListings Page</>
}
const mapStateToProps = (state: any) => {
return {
reduxState: state.reduxState.reduxState,
}
}
const mapDispatchToProps = {
setReduxState: reduxStateOperations.setReduxState
}
const connector = Redux.connect(mapStateToProps, mapDispatchToProps);
export default connector(AppListings);
你有沒有研究過使用React.Context
創建一個全局提供者(或者專門針對你的布局)? 通過這種方式,您可以將您的操作存儲在減速器中。
鈎子/使用上下文.jsx
import React from 'react'
// Set a relevant initial state
const INITIAL_STATE = {}
// Add actions for your application
const ACTIONS = {
UPDATE_VAR: 'update-var',
DELETE_VAR: 'delete-var'
}
const reducer = (state, action) => {
const next = { ...state } // Shallow copy
switch (action.type) {
case ACTIONS.UPDATE_VAR:
next.var = action.data
break
case ACTIONS.DELETE_VAR:
delete next.var
break
}
return next
}
const Context = React.createContext({
state: { ...INITIAL_STATE },
dispatch: () => null
})
export const Provider = ({ children }) => {
const [state, dispatch] = React.useReducer(
reducer, init(false)
)
return (
<Context.Provider value={[state, dispatch, ACTION]}>
{children}
</Context.Provider>
)
}
// Rename to something relevant
const useContext = () => {
const [state, dispatch] = React.useContext(Context)
return [state, dispatch]
}
export default useContext
pages/index.jsx :提供 state
import { Provider } from '../hooks/use-context'
const Page = () => (
<Provider>
<AppActions />
<AppListings />
</Provider>
)
export default Page
components/app-actions.jsx :更新 state
import useContext from '../hooks/use-context'
const AppActions = () => {
const [, dispatch] = useContext()
return (
<button onClick={() => dispatch(/* action */)>
Click Me
</button>
)
}
export default AppActions
components/app-listings.jsx :使用 state
import useContext from '../hooks/use-context'
const AppListings = () => {
const [state] = useContext()
return (
<pre>{JSON.stringify(state, null, 2)</pre>
)
}
export default AppListings
您還可以查看第三方解決方案,例如Redux 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.