
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@bubblydoo/angular-react
Advanced tools
This is a small Angular library that lets you use React components inside Angular projects.
This is a small Angular library that lets you use React components inside Angular projects.
<react-wrapper [component]="Button" [props]="{ children: 'Hello world!' }">
function ReactComponent({ text }) {
return <AngularWrapper component={TextComponent} inputs={{ text }}>
}
npm i @bubblydoo/angular-react
import { AngularReactModule } from '@bubblydoo/angular-react'
@NgModule({
...,
imports: [
...,
AngularReactModule
]
})
ReactWrapperComponent
Use this component when you want to use React in Angular.
It takes two inputs:
component
: A React componentprops?
: The props you want to pass to the React componentThe React component will be first rendered on ngAfterViewInit
and rerendered on every ngOnChanges
call.
import Button from './button.tsx';
@Component({
template: `<react-wrapper [component]="Button" [props]="{ children: 'Hello world!' }">`
})
class AppComponent {
Button = Button
}
AngularWrapper
Use this component when you want to use Angular in React.
It takes a few inputs:
component
: An Angular componentinputs?
: The inputs you want to pass to the Angular component, in an objectoutputs?
: The outputs you want to pass to the Angular component, in an objectevents?
: The events from the Angular component to listen to, using addEventListener
. Event handlers are wrapped in NgZone.run
ref?
: The ref to the rendered DOM element (uses React.forwardRef
)import { TextComponent } from './text/text.component.ts'
function Text(props) {
return (
<AngularWrapper
component={TextComponent}
inputs={{ text: props.text }}
events={{ click: () => console.log('clicked') }}/>
)
}
useInjected
The Angular Injector is provided on each React component by default using React Context. You can use Angular services and other injectables with it:
import { useInjected } from '@bubblydoo/angular-react'
const authService = useInjected(AuthService)
useObservable
Because consuming observables is so common, we added a helper hook for it:
import { useObservable, useInjected } from '@bubblydoo/angular-react'
function LoginStatus() {
const authService = useInjected(AuthService)
const [value, error, completed] = useObservable(authService.isAuthenticated$)
if (error) return <>Something went wrong!<>
return <>{value ? "Logged in!" : "Not logged in"}</>
}
Because this library creates a different ReactDOM root for each react-wrapper
, if you want to have a global React Context, you can register it as follows:
// app.component.ts
constructor(angularReact: AngularReactService) {
const client = new ApolloClient();
// equivalent to ({ children }) => <ApolloProvider client={client}>{children}</ApolloProvider>
angularReact.addWrapper(({ children }) => React.createElement(ApolloProvider, { client, children }));
}
In this example, we use ApolloProvider
to provide a client to each React element. We can then use useQuery
in all React components.
You can test the functionality of the components inside a local Storybook:
npm run storybook
If you want to use your local build in an Angular project, you'll need to build it:
npm run build
Then, use npm link
:
cd dist/angular-react
npm link # this will link @bubblydoo/angular-react to dist/angular-react
In your Angular project:
npm link @bubblydoo/angular-react
node_modules/@bubblydoo/angular-react
will then be symlinked to dist/angular-react
.
See this blog post for the motivation and more details: Transitioning from Angular to React, without starting from scratch
FAQs
Use React in Angular and Angular in React, easily
The npm package @bubblydoo/angular-react receives a total of 233 weekly downloads. As such, @bubblydoo/angular-react popularity was classified as not popular.
We found that @bubblydoo/angular-react demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.