我正在查看https://codesandbox.io/s/new中的默认演示 React 代码,结果如下:
import React from 'react';
import { render } from 'react-dom';
import Hello from './Hello';
const App = () => <Hello name="CodeSandbox" />;
render(<App />, document.getElementById('root'));
Run Code Online (Sandbox Code Playgroud)
这与我见过的许多其他示例不同,这些示例通常更像是:
class Layout extends React.Component {
constructor(props) {
super(props);
this.state = {...};
}
render() {...}
}
Run Code Online (Sandbox Code Playgroud)
由于 codeandbox 示例没有类或构造函数,因此我没有地方放置this.state. 那么我将如何以及在哪里添加状态?
大多数 React 文件的顶部有:
import React from 'react'
Run Code Online (Sandbox Code Playgroud)
但react实际上并不是一个文件。那么它在哪里以及如何从无到有导入呢?
基本上,我想这样做:
<script src=" path + '/jquery.js'"></script>
Run Code Online (Sandbox Code Playgroud)
(显然这不起作用.)
我有一个独立页面加载外部脚本<script>,但URL需要以变量为前缀path(因为页面在不同环境中的不同服务器上运行).我很容易path从URL中找到"?path = X ",其中X是实际路径.
如果我有jQuery,我知道我可以getScript()用来动态加载外部.js文件,但jQuery是我需要加载的文件之一!我不需要在这里过于复杂,不需要担心编码或文件类型,除了更改服务器之外,这是一个相当简单的情况.
假设我有一个 freemarker 宏:
<#macro helloObject>
World
</#macro>
Run Code Online (Sandbox Code Playgroud)
在某些情况下,我可以轻松调用它:
Hello, <@helloObject/>!
Run Code Online (Sandbox Code Playgroud)
但是插入它的语法是什么,例如,如果它是字符串的一部分?
<#assign greeting="Hello, <@helloObject>!" /> <#-- doesn't work -->
<#assign greeting="Hello, ${helloObject}!" /> <#-- doesn't work -->
<#assign greeting="Hello, ${@helloObject}!" /> <#-- doesn't work -->
<#assign greeting="Hello, ${<@helloObject/>}!" /> <#-- doesn't work -->
<#assign greeting="Hello, @helloObject!" /> <#-- doesn't work -->
${greeting}
Run Code Online (Sandbox Code Playgroud) template-engine freemarker templating templating-engine string-interpolation
使用PHP一些代码,我遇到了一些代码,基本上检查相同的变量是否为空两次:
if ( !empty( $_GET[ 'branch' ] ) ) {
$branch = $_GET[ 'branch' ];
}
if ( empty( $branch ) ) {
_output( 'Error: no branch specified!' );
exit( 1 );
}
Run Code Online (Sandbox Code Playgroud)
知道为什么这样设置吗?与使用相比else,有什么优势,比如:
if ( !empty( $_GET[ 'branch' ] ) ) {
$branch = $_GET[ 'branch' ];
} else {
_output( 'Error: no branch specified!' );
exit( 1 );
}
Run Code Online (Sandbox Code Playgroud) reactjs ×2
freemarker ×1
include ×1
is-empty ×1
javascript ×1
jquery ×1
php ×1
state ×1
templating ×1
webpack ×1