# Using Builder with Next.js
## Getting started
### Create an account
Frist things first, if you don't yet have one, create a free account at [Builder.io](https://builder.io)
### To install
`npm install --save @builder.io/react @builder.io/widgets`
### Add the components and getInitialProps
To any page in next.js, you can fetch for a Builder page, and handle wether builder found a
match (or possibly not).
For instance, if you know you want /about to be a Builder page, create a Builder page with url /about
and use the following for `pages/about.js`
```js
import React from 'react'
import { builder, BuilderComponent } from '@builder.io/react'
import '@buidler.io/widgets';
builder.init(BUILDER_API_KEY)
class About extends React.Component {
static async getInitialProps({ res, req }) {
// If there is a Builder page for this URL, this will be an object, otherwise it'll be null
const page = await builder.get('page', { req, res }).promise()
return { builderPage: page }
}
render() {
return (
<div>
{this.props.builderPage ? (
<BuilderComponent name="page" content={this.props.builderPage} />
) : (
/* Show something else */
)}
</div>
)
}
}
```
### Add the URL to your builder model and get editing
Next, when you have added the Builder page component on your local site, go to the [models](https://buidler.io/models) page in Builder, choose the "page" model, and give it a URL where it can find this React component for the "preview url" field. This may be, for example, `http://localhost:8888/my-page`
<img src="https://i.imgur.com/PRWvNM1.gif">
Now go to the [content](https://buidler.io/content) page, choose "+ new entry", and create a new page. This will open up that preview URL you previously gave and let you use the visual editor.
<img src="https://imgur.com/5BC0lYR.gif">
Also, once this is all working as expected and deployed, make sure to change the preview URL to one on your production site so anyone you acllow on your team can build and create pages!
Also, when done creating pages, be sure to hit "publish" to publish that content to be live!
### Troubleshooting
When creating a page in Builder, if Builder says "Builder code not found", that means Builder can't find the `<BuilderComponent />` in your React app. Try using your React devtools to ensure that component is in fact showing up. Even try hardcoding it temporarily to test that Builder can find it and you can use the visual editor on your site.
If anything else ever goes wrong for you, chat us anytime form the bottom right corner at [builder.io](https://builder.io) or email steve@builder.io. We are always happy to help!
## Dynamic landing pages
One of Builder's most powerful use cases is allowing the creation of dynamic new pages with their own unique URLs.
Since next.js doesn't natively support dynamic pages, we have a couple of options.
First, and perhaps most elegant, is to use [next-routes](https://github.com/fridays/next-routes)
### With next-routes
```js
// routes.js
module.exports = routes()
// ... your other routes
// Be sure this is last so your other routes always match first
.add('/*', 'builder')
```
```js
// pages/builder.js
import { Component } from 'react'
import { builder, BuilderComponent } from '@builder.io/react'
// Allow interactive widgets in the editor (importing registers the react components)
import '@buidler.io/widgets'
import Error from './_error'
builder.init(YOUR_API_KEY)
class Builder extends Component {
static async getInitialProps({ req, res }) {
const page = await builder.get('page', { req, res }).promise()
if (!page && res) res.statusCode = 404
return { data }
}
render() {
const { page } = this.props
if (!page) return <Error status={404} />
return <BuilderComponent name="page" content={page} />
}
}
```
### With pages/\_error.js
A simplistic approach could also be to use \_error.js. Since \_error.js functions as catchall page, we can add our own handling:
```js
import React from 'react'
import { builder, BuilderComponent } from '@builder.io/react'
import '@buidler.io/widgets'
builder.init(BUILDER_API_KEY)
class CatchallPage extends React.Component {
static async getInitialProps({ res, req }) {
// If there is a Builder page for this URL, this will be an object, otherwise it'll be null
const page = await builder.get('page', { req, res }).toPromise()
if (res && res.statusCode === 404 && page) {
res.statusCode = 200
}
return { builderPage: page }
}
render() {
return (
<div>
{this.props.builderPage ? (
<BuilderComponent name="page" content={this.props.builderPage} />
) : (
'Error!'
)}
</div>
)
}
}
export default CatchallPage
```
See `examples/next-js/pages/_error.js` for a real example you can run.
### Custom
Alternatively, you can add some custom behavior in your `server.js` or another routing library of your choice.
Just follow the same behavior as the previous examples - if a URL is not found in your next.js routes, check for a Builder page with `await builder.get('page').promise()` at that URL, and if found render `<BuilderComponent name="page" content={page} />` like in the above examples
## Using your React components in Builder pages
You can use your React components in the drag and drop editor in Builder. Simply wrap the component as shown below:
```js
import { BuilderBlock } from '@builder.io/react'
@BuilderBlock({
name: 'Simple Text',
inputs: [{ name: 'text', type: 'string' }]
})
export class SimpleText extends React.Component {
render() {
return <h1>{this.props.text}</h1>
}
}
```
And then be sure to import this component wherever you want it to be accessible in the editor
For more information on using your React components in Builder, including
detail on the different input types and options, see our detailed docs [here](https://builder.io/c/docs/custom-react-components)
For lots of examples of using React components in Builder, see the source for our built-in Builder blocks [here](https://github.com/BuilderIO/builder/tree/master/packages/react/src/blocks) and widgets [here](https://github.com/BuilderIO/builder/tree/master/packages/widgets/src/components)
```js
// As long as this is imported on the same page as your <BuilderComponent> is used,
// you will have access to this component in the drag and drop editor
import './your-builder-component'
// ...
export default () => <BuilderComponent name="page" />
```
And then it will show up in the insert menu (under "show more") in the Builder editor!
For the `@BuilderBlock` decorator support you need to be using typescript or babel with legacy decorators. Alternatively you can use the alternative syntax:
```js
import { BuilderBlock } from '@builder.io/react'
class SimpleText extends React.Component {
render() {
return <h1>{this.props.text}</h1>
}
}
BuilderBlock({
name: 'Simple Text',
inputs: [{ name: 'text', type: 'string' }]
})(SimpleText)
```
没有合适的资源?快使用搜索试试~ 我知道了~
react-builder采用拖拽方式构建页面使用您的React组件进行设计远程发布
共519个文件
ts:130个
json:68个
tsx:66个
需积分: 32 22 下载量 4 浏览量
2019-08-15
02:35:06
上传
评论 1
收藏 1.6MB ZIP 举报
温馨提示
builder - 采用拖拽方式构建页面,使用您的React组件进行设计,远程发布
资源推荐
资源详情
资源评论
收起资源包目录
react-builder采用拖拽方式构建页面使用您的React组件进行设计远程发布 (519个子文件)
gradlew.bat 3KB
gradlew.bat 2KB
BUCK 2KB
BUCK 1KB
BUCK 152B
.buckconfig 114B
build_defs.bzl 602B
styles.css 882B
App.css 492B
App.css 468B
index.css 380B
index.css 366B
index.css 269B
index.css 266B
styles.css 80B
styles.css 80B
builder-blocks.component.css 32B
builder-content.component.css 28B
builder-component.component.css 27B
app.component.css 0B
app.component.css 0B
.flowconfig 3KB
.gitattributes 66B
.gitattributes 16B
.gitattributes 15B
.gitignore 817B
.gitignore 544B
.gitignore 529B
.gitignore 430B
.gitignore 316B
.gitignore 310B
.gitignore 237B
.gitignore 148B
.gitignore 106B
.gitignore 104B
.gitignore 104B
.gitignore 104B
.gitignore 104B
.gitignore 104B
.gitignore 29B
.gitignore 29B
.gitkeep 0B
.gitkeep 0B
.gitkeep 0B
.gitkeep 0B
.gitkeep 0B
.gitkeep 0B
build.gradle 8KB
build.gradle 6KB
build.gradle 993B
build.gradle 974B
settings.gradle 190B
settings.gradle 50B
gradlew 6KB
gradlew 5KB
AppDelegate.h 386B
AppDelegate.h 325B
index.html 2KB
index.html 2KB
index.html 2KB
index.html 531B
index.html 531B
builder-component.component.html 445B
index.html 302B
index.html 302B
index.html 294B
index.html 231B
app.component.html 210B
builder-blocks.component.html 115B
app.component.html 70B
builder-content.component.html 27B
favicon.ico 15KB
favicon.ico 5KB
favicon.ico 5KB
favicon.ico 5KB
favicon.ico 4KB
favicon.ico 4KB
gradle-wrapper.jar 54KB
gradle-wrapper.jar 53KB
MainApplication.java 1KB
MainApplication.java 1KB
MainActivity.java 369B
MainActivity.java 357B
chunk-d6e55dce.js 140KB
chunk-6cf7a920.js 124KB
builder-webcomponents.js 34KB
chunk-82b7d41d.js 28KB
webpack.config.prod.js 21KB
webpack.config.dev.js 18KB
chunk-3ccdbe6e.js 16KB
chunk-af8d4732.js 12KB
build.js 6KB
webpackDevServer.config.js 6KB
serviceWorker.js 5KB
serviceWorker.js 5KB
builder-webcomponents-async.js 4KB
start.js 4KB
env.js 3KB
App.js 3KB
paths.js 3KB
共 519 条
- 1
- 2
- 3
- 4
- 5
- 6
资源评论
weixin_39841856
- 粉丝: 487
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功