react-native-action-cable
react-native-action-cable copied to clipboard
Possibility to pass headers argument as function
First of all, thanks for creating this repository.
In the project I'm working on I'd like to switch from passing an Auth Token as URL parameter to header. Currently we keep the client's initialisation in custom React Hook that returns both client and setter function, so that we can set the token from outside.
This works due to nature of createConsumer - it accepts both a value or a function (here, websocketUrl
) that is being lazily evaluated, when the token is already granted. Changing to header-based authentication won't work currently, as headers require immediate object value.
export default function useCreateApolloClient {
const tokenRef = useRef(null)
const [token, setToken] = useState(tokenRef.current)
// ...
if (!connection.current) {
connection.current = createClientWithAuth(tokenRef)
}
return [connection.current.client, setToken]
}
function createClientWithAuth(tokenRef) {
const actionCable = ActionCable.createConsumer(websocketUrl(tokenRef))
// ...
return { client, actionCable }
}
const websocketUrl = (tokenRef) => () =>
tokenRef.current ? `${URL}?token=${tokenRef.current}` : URL
I'm not that familiar with React philosophy and it might sound like specific use case, but would it be valuable (especially for React Hooks) to also have a possibility to lazily evaluate headers same way URL argument does?