r/Clojurescript • • Aug 07 '22

React Native with Tailwind in ShadowJS

I'm stuck trying to add tailwind to my react native project.

I have create a repo with the problem: mattrybin/clojurescript-tailwind-with-react-native

What I'm trying todo is to add vadimdemedes/tailwind-rn to my clojurescript project.

I do all the steps until step 7: Use Tailwind in React Native!

import {useTailwind} from 'tailwind-rn';

const MyComponent = () => {

const tailwind = useTailwind();

return <Text style={tailwind('text-blue-600')}>Hello world</Text>;

};

I'm new to clojure so this is my attempt based this article: react-hooks-raw

(ns example.components.my-component

(:require ["react-native" :as rn]

["tailwind-rn" :as tailwind]))

(defn text [text]

(let [tw tailwind/useTailwind]

[:> rn/View (:style (tw "flex-1"))

[:> rn/Text text]]))

I get the error: Error: Invalid hook call. Hooks can only be called inside of the body of a function component.

How can I use useTailwind in my project?

7 Upvotes

7 comments sorted by

View all comments

Show parent comment

3

u/vadimdemedes Aug 07 '22

tailwind-rn author here.

Common problem I saw is that people forget to wrap the component they're using useTailwind hook in inside a TailwindProvider. TailwindProvider should ideally be at the root of your component tree.

For example:

// Somewhere in your components
const MyComponent = () => {
  const tailwind = useTailwind();

  return <View style={tailwind('pt-12')}>...</View>;
}

// In your root file
import utilities from './tailwind.json';

const RootComponent = () => (
  <TailwindProvider utilities={utilities}>
    <MyComponent/>
  </TailwindProvider>
);

Hope that helps.