https://www.growthbook.io/ logo
#announcements
Title
# announcements
h

helpful-carpenter-3208

03/14/2022, 7:49 AM
Any suggestions on using feature with Nextjs SSR mode? I met unknownFeature error when use feature..
f

fresh-football-47124

03/14/2022, 7:50 AM
sure, want to share some code (or via DM)?
are you using the Provider?
h

helpful-carpenter-3208

03/14/2022, 7:58 AM
correct. I just realize it’s my own code bug about setting wrong feature data.. sorry for bother here😇
f

fresh-football-47124

03/14/2022, 8:09 AM
_app.tsx/js
Copy code
import { GrowthBook, GrowthBookProvider } from "@growthbook/growthbook-react";

const growthbook = new GrowthBook({
  realtimeKey: "key_NNNN",
  trackingCallback: (experiment, result) => {
    // change to your tracking call:
    track("Experiment Viewed", {
      experimentId: experiment.key,
      variationId: result.variationId,
    });
  },
});

//... inside of App:

useEffect(() => {
    // Load feature definitions JSON from GrowthBook API
    fetch("<https://cdn.growthbook.io/api/features/key_NNNN>")
      .then((res) => res.json())
      .then((json) => {
        growthbook.setFeatures(json.features);
      })
      .catch(() => {
        console.log("Failed to fetch GrowthBook feature definitions");
      });
  }, [router.pathname]);

// ...

return ( <GrowthBookProvider growthbook={growthbook}>
//...
</GrowthBook>);
inside your components:
Copy code
import { useFeature } from "@growthbook/growthbook-react";

//...

 const config = useFeature("somefeature-config").value;
1
29 Views