Due my current work task i have a case – to call a modal created as a React-component from SharePoint Ribbon. In two words, SharePoint ribbon is a structure that contains buttons and js-functions binded for it. When you push the button, js function binded for it calls… hope you understand :). And i started to google: “how to interract with react module from outer js, how to change react state from outer js, force re-render react component from pure js” and so on…
So, the main problem is: you cannot bind react-component function to ribbon button because it hides deep inside into webpack js bundle. Also, i need to change React Module state because i want rerender component when i click the button.
So, i make the fiddle and looks like it works correctly – https://jsfiddle.net/o6ujb5g2/. It is not SharePoint ribbon, but almost the same.
Idea is to attach function from react modal to global window object. This object exist from almost anywhere.
Steps:
- I created a stub-function in pure js for window.func that i want to override from component (it is for safety, if some problems with component will happens due it’s render)
- In component i initialize a func with business logic, that changes component state
- On componentDidMount i override global window.func to module.func
- PROFIT!!!