I have the below code and I have an issue with calling API call using usecontext in react. Guide me to fix it.
https://codesandbox.io/s/serene-williamson-bbik6?file=/src/App.js
import ReactDOM from "react-dom";
import "./styles.css";
const JediContext = React.createContext();
function Display() {
const value = useContext(JediContext);
return <div>{value.total}, I am your Father.</div>;
}
function App() {
const [data, setData] = useState(0);
fetch("https://api.npms.io/v2/search?q=react")
.then((response) => response.json())
.then((rawData) => {
console.log(data);
setData(rawData);
});
return (
<JediContext.Provider value={data.total}>
<Display />
</JediContext.Provider>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
1 Answer 1
There are a few problems with the posted code:
- You are accessing
totalin theDisplaycomponent, while passingdata.totalin theJediContext.Provider. I think you intended to pass justdata. fetchruns with every render so your page with keep re-rendering since you aresetData(rawData)afterfetchis done. This will cause an infinite loop and crash the application. It is common practice (at this point in time), to useuseEffectto fetch data. In the future,suspense1 might be used for this purpose but that's experimental now.- One of the error you were getting was because
JediContextdidn't have a default value. Sovalue.totalproduces a null pointer exception. To provide a default value to the context,const JediContext = React.createContext(/*provide default value here*/); - You already have the
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
in your index.js. In App.js, you just need to export App as a default function.
Here's a version of your code that is kinda working in Codesand box: https://codesandbox.io/s/infallible-worker-bcix4?file=/src/App.js
answered Jan 2, 2022 at 17:18
Hanchen Jiang
2,6702 gold badges12 silver badges21 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js
useState({total: 0});to set the initial value for the very first render. I encountered a second problem but I don't think it's related.