Why react-rails can not work with react context API?
See original GitHub issuereact-rails can not re-render provider new state in children, also can not work with useContext in react
Contributing Guide
Steps to reproduce
<%= react_component("Test")%>
Test.js
import React from 'react';
import { UserProvider, UserConsumer } from './context';
import Panel from './demo';
const Test = () => {
return (
<UserProvider>
<Panel />
</UserProvider>
)
}
export default Test
context.js
import React from 'react';
let UserContext;
const { Provider, Consumer } = UserContext = React.createContext();
// Context.Consumer, Context.Provider
class UserProvider extends React.Component {
state = {
currentUser: "foo"
};
componentDidMount() {
setTimeout(() => {
this.setState({
currentUser: "bar"
})
}, 6000)
}
render() {
return (
<Provider
value={{
user: this.state.currentUser,
}}
>
{this.props.children}
</Provider>
);
}
}
export { UserProvider, Consumer as UserConsumer, UserContext };
demo.js
import React, { useContext } from 'react';
import { UserContext } from './context';
const Panel = () => {
const { user } = useContext(UserContext);
return (
<p>{user}</p>
)
}
export default Panel
Expected behavior
Work with useContext and children can re-render when provider state is updated
Actual behavior
react.development.js:88 Uncaught Invariant Violation: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
- You might have mismatching versions of React and the renderer (such as React DOM)
- You might be breaking the Rules of Hooks
- You might have more than one copy of React in the same app
System configuration
Sprockets or Webpacker version: 3.5 React-Rails version: 2.4.4 Rect_UJS version: 2.4.4 Rails version: 5.2 Ruby version: 2.3.6
(Describe your issue here)
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Why react-rails can not work with react context API?
react-rails can not re-render provider new state in children, also can not work with useContext in react. Contributing Guide ...
Read more >Problems with Context Api React - Stack Overflow
1 Answer 1 · Thanks! No longer returns the error, but unable to access the object. In the console. · I was able...
Read more >The Problem with React's Context API | Strings and Things
This is affectionately known as “prop-drilling”, and used to be a huge pain point in React. The Context API makes short work of...
Read more >React on Rails: Building a Simple App - Honeybadger.io
Each one does one or more tasks that make sense as a context. In short, a component is made of a JavaScript class...
Read more >Context - React
API. React.createContext; Context.Provider; Class.contextType; Context. ... Context is designed to share data that can be considered “global” for a tree of ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hooks and context api happen within the JS context. This issue is likely not related to react-rails. Basically if it works in Webpack it will work in this gem broadly speaking.
Looking at the original question the OP could be using an old version of React if they’re using Sprockets (They didn’t specify) that doesn’t have hooks. https://github.com/reactjs/react-rails/blob/master/VERSIONS.md
@mischa-s the best way to contribute to this specific issue would be to replicate this issue in a new project and link it to this issue so we can start to diagnose it more fully. Thank you for the assistance, it can take me a long time to get around to solving issues on topics I don’t personally use otherwise.
I would be keen to help work adding support for hooks but don’t really know where to start - anybody got any ideas?