无效的挂钩调用。在 WordPress 的管理部分使用 ReactJS(特别是编辑自定义帖子类型)
我想在 WordPress 中创建一个基于 React 的自定义帖子类型编辑器。我创建了一个插件,它:
- 注册自定义帖子类型
my-custom-post
- 将
注入 < code>/wp-admin/post-new.php?post_type=my-custom-post
- 构建一个简单的 React 应用程序,在
/wp-admin/plugins/my-custom-plugin/src/index.js
const {
useState
} = wp.element;
function Example(props) {
const [value, setValue] = useState(new Date());
return <h1>Hello!</h1>;
}
render(
<Example />,
document.getElementById( 'root' )
);
依赖项
λ yarn list --pattern react
yarn list v1.22.15
├─ @babel/[email protected]
├─ @babel/[email protected]
├─ @babel/[email protected]
├─ @babel/[email protected]
├─ @babel/[email protected]
├─ @babel/[email protected]
├─ @pmmmwh/[email protected]
├─ @svgr/[email protected]
├─ @types/[email protected]
├─ @types/[email protected]
├─ @wojtekmaj/[email protected]
├─ [email protected]
│ └─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
│ └─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
└─ [email protected]
< strong>wp-content/plugins/my-custom-plugin/package.json
{
"name": "my-custom-plugin",
"version": "1.0.0",
"description": "I build, therefore I'm awesome.",
"scripts": {
"build": "wp-scripts build",
"format": "wp-scripts format",
"lint:css": "wp-scripts lint-style",
"lint:js": "wp-scripts lint-js",
"start": "wp-scripts start",
"packages-update": "wp-scripts packages-update"
},
"engines": {
"node": ">=8.0.0",
"npm": ">=6.0.0"
},
"devDependencies": {
"@wordpress/scripts": "^20.0.2"
},
"dependencies": {
"react-multi-date-picker": "^3.3.0"
}
}
错误
react-dom.min.js?ver=17.0.1:141 Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
I want to create a React based custom post type editor in WordPress. I've created a plugin that:
- Registers a custom post type
my-custom-post
- Injects
<div id="root"></div>
into/wp-admin/post-new.php?post_type=my-custom-post
- Builds a simple React app that renders a component in
<div id="root"></div>
/wp-admin/plugins/my-custom-plugin/src/index.js
const {
useState
} = wp.element;
function Example(props) {
const [value, setValue] = useState(new Date());
return <h1>Hello!</h1>;
}
render(
<Example />,
document.getElementById( 'root' )
);
Dependencies
λ yarn list --pattern react
yarn list v1.22.15
├─ @babel/[email protected]
├─ @babel/[email protected]
├─ @babel/[email protected]
├─ @babel/[email protected]
├─ @babel/[email protected]
├─ @babel/[email protected]
├─ @pmmmwh/[email protected]
├─ @svgr/[email protected]
├─ @types/[email protected]
├─ @types/[email protected]
├─ @wojtekmaj/[email protected]
├─ [email protected]
│ └─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
│ └─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
└─ [email protected]
wp-content/plugins/my-custom-plugin/package.json
{
"name": "my-custom-plugin",
"version": "1.0.0",
"description": "I build, therefore I'm awesome.",
"scripts": {
"build": "wp-scripts build",
"format": "wp-scripts format",
"lint:css": "wp-scripts lint-style",
"lint:js": "wp-scripts lint-js",
"start": "wp-scripts start",
"packages-update": "wp-scripts packages-update"
},
"engines": {
"node": ">=8.0.0",
"npm": ">=6.0.0"
},
"devDependencies": {
"@wordpress/scripts": "^20.0.2"
},
"dependencies": {
"react-multi-date-picker": "^3.3.0"
}
}
The Error
react-dom.min.js?ver=17.0.1:141 Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论