gho*_*ost 5 javascript reactjs react-router isomorphic-javascript server-side-rendering
我正在尝试制作一个 SSR 反应应用程序,但无法将道具从 express 传递到组件。我在做什么错误?
服务器.js
import AppPage2 from './src/project02/LandingPage';
......
......
server.get('/project2',async (req,res)=>{
    const context = {data:'test'}
    const sheet = new ServerStyleSheet();
    const content = ReactDOMServer.renderToString(sheet.collectStyles(
        <StaticRouter location={req.url} context={context}>
            <AppPage2 state={{"foo":"baar"}}/>
        </StaticRouter>)
    );
    const styles = sheet.getStyleTags();
    let html = appShell( 'Project 2', content,' project2.bundle.js',styles)
    res.status(200).send(html);
    res.end()
})
Run Code Online (Sandbox Code Playgroud)
AppPage2(./src/project02/LandingPage)
import React from 'react';
import {Wrapper,Title}  from './styleComponent/testStylePage'
.......
.......
class AppPage extends React.Component {
    componentDidMount() {
       console.log("{{API}}",this,this.props, this.props.staticContext)
    }
    render(){
        return(
            <Wrapper>
                <Title>This is project 01 </Title>
            </Wrapper>
        )
    }
}
export default AppPage;
Run Code Online (Sandbox Code Playgroud)
索引.js
import React from 'react';
import ReactDOM from 'react-dom';
import AppPage from './project02/LandingPage'
ReactDOM.hydrate(
        <AppPage></AppPage>,
    document.querySelector('#root')
);
Run Code Online (Sandbox Code Playgroud)
webpack.client.conf
const path = require('path');
const glob = require("glob");
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const entry = glob.sync("src/*.js")
    .reduce((x, y) => Object.assign(x, 
        {
            [y.replace('src/','').replace('.js','')]: `./${y}`,
        }
), {});
    
    module.exports = {
            target: 'node',
            mode: 'development',//development,production
            entry: entry,
            output: {
                filename:'[name].bundle.js',
                path: path.resolve(__dirname,'build/public/'),
                publicPath: '/build/'
            },
            module: {
                rules: [
                    {
                        test: /\.js$/,
                        exclude: /(node_modules)/,
                        use: {
                            loader: 'babel-loader'
                        },
                        
                    },
                ]
            },
            plugins: [
                // new BundleAnalyzerPlugin()
            ]
        }
Run Code Online (Sandbox Code Playgroud)
我无法从 AppPage2(./src/project02/LandingPage) 这个页面记录 console.log("{{API}}",this,this.props, this.props.staticContext) 这个页面我正在从服务器发送数据。 js(快递)
小智 1
你的 props 已经传递到了 Page2 上的组件,但是你渴望使用一个永远不会被调用的函数,
componentDidMount()在组件安装(插入到 DOM 树中)后立即调用。
在你的情况下,你没有将任何东西挂载到 DOM 上,因为 React 只会将你的组件渲染为 html 并且不会等待你的组件挂载,实际上你的 NodeJs 服务器中没有 DOM,你只渲染组件并将其作为字符串返回,而不是将其插入到 DOM 中。
您的道具已经存在,您可以在类构造函数和渲染方法中控制台记录它们:
class AppPage extends React.Component {
    constructor (props){
        super(props)
        console.log("{{API}}",this,this.props, this.props.staticContext)
    }
    render(){
        //or console log it here
        console.log("{{API}}",this,this.props, this.props.staticContext)
        return(
            <Wrapper>
                <Title>This is project 01 </Title>
            </Wrapper>
        )
    }
}
Run Code Online (Sandbox Code Playgroud)
在这种状态下,你的组件将挂载而不是挂载,你也可以使用控制台记录你的道具UNSAFE_componentWillMount,但正如它的名字所说,它是不安全的
ReactJS.org
您还可以创建自己的函数,它会起作用:
myFunction () {
   console.log(this.props.state)
}
myFunction();
Run Code Online (Sandbox Code Playgroud)
        |   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           193 次  |  
        
|   最近记录:  |